TableView Checklist

Discussion in 'iPhone/iPad Programming' started by GhostDZ9, May 24, 2011.

  1. macrumors regular

    Joined:
    Sep 13, 2010
    #1
    Hey guys so part of my app is a checklist. The table is shown as a group rather then plain.

    So basically every time they select a row it puts the accessoryCheckmark at the end of the row and removes the checkmark from everywhere else.

    Is there a method or a function that basically works like

    Code:
    if (indexPath.row isSelected) {
        cell.AccessoryType = UITableViewAccessoryCheckmark;
    } else
        cell.AccessoryType = UITableViewAccessoryNone;
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I would suggest adding a Boolean flag to the data source model you use to back your table view. Then you can check against that.
     
  3. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #3
    How would I go about doing that?
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Maybe explain how you have your data source set up.
     
  5. thread starter macrumors regular

    Joined:
    Sep 13, 2010
    #5
    Ive actually figured it out, I created an int var and then did this code

    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        
        static NSString *CellIdentifier = @"Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    	}
    	
    	
    	if (indexPath.row == selectedRow){
    		cell.accessoryType = UITableViewCellAccessoryCheckmark;
    	} else {
    		cell.accessoryType  = UITableViewCellAccessoryNone;
    	}
    	cell.textLabel.text = [rows objectAtIndex:indexPath.row];
    	
    	return cell;
    }
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    	selectedRow = indexPath.row;
    	[self.tableView reloadData];
    
    
    }
     

Share This Page