PDA

View Full Version : TableView Checklist




GhostDZ9
May 24, 2011, 08:57 AM
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

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



dejo
May 24, 2011, 09:53 AM
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.

GhostDZ9
May 24, 2011, 09:55 AM
How would I go about doing that?

dejo
May 24, 2011, 10:14 AM
How would I go about doing that?

Maybe explain how you have your data source set up.

GhostDZ9
May 24, 2011, 10:58 AM
Ive actually figured it out, I created an int var and then did this 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];


}