我的 didSelectRowAtIndexPath: 方法中有以下代码,我希望用户能够点击单元格,让单元格显示复选标记,并将选定的单元格添加到单独的数组中。我还希望用户能够点击已选择的单元格,然后将取消选中标记并将其从数组中删除。
-(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]);
}
NSLog 输出以下内容:
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.
更新:
这是控制台显示的内容:
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.
这是代码:
-(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];
}