I have the following code in my didSelectRowAtIndexPath: method, and I want the user to be able to tap on a cell, have the cell show the checkmark, and add the selected cells to a separate array. I also would like the user to be able to tap an already selected cell, which will then take away the checkmark and remove it from the array.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.setupTable deselectRowAtIndexPath:indexPath animated:YES];
SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Feed name: %@", cell.setupFeedName.text);
if ([self.selectedCells containsObject:cell.setupFeedName.text]) {
[self.selectedCells removeObjectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:cell.setupFeedName.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[self.setupTable reloadData];
NSLog(@"%i cells are selected.", [self.selectedCells count]);
NSLog(@"%i cells are not selected.", [self.setupFeeds count]);
}
The NSLog outputs the following:
2013-08-12 07:04:58.767 [13921:c07] Feed name: Politico
2013-08-12 07:04:58.768 [13921:c07] 0 cells are selected.
2013-08-12 07:04:58.769 [13921:c07] 6 cells are not selected.
UPDATE:
Here is what Console shows:
2013-08-12 07:31:40.390[14117:c07] Feed name: Huffington Post
2013-08-12 07:31:40.392[14117:c07] 5 cells are selected.
2013-08-12 07:31:40.392[14117:c07] 6 cells are not selected.
Here is the code:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.setupTable deselectRowAtIndexPath:indexPath animated:YES];
SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Feed name: %@", cell.setupFeedName.text);
if ([[self.selectedCells objectAtIndex:indexPath.row] isEqualToString:cell.setupFeedName.text]) {
[self.selectedCells removeObjectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:cell.setupFeedName.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(@"%i cells are selected.", [self.selectedCells count]);
NSLog(@"%i cells are not selected.", [self.setupFeeds count]);
[self.setupTable reloadData];
}