我有一个 TableView,我想在它被点击时为一行添加复选标记。为此,我有:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCountry = [self.files objectAtIndex:indexPath.row];
NSString *Documents = [[NSBundle mainBundle] pathForResource:selectedCountry ofType:@"ppt" inDirectory:@"thepowerpoints"];
//NSLog(@"%@", selectedCountry);
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
if (newCell.accessoryType == UITableViewCellAccessoryNone) {
if (self.chosen == nil) {
self.chosen = [[NSMutableArray alloc] init];
}
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.chosen addObject:Documents];
NSLog(@"%@", self.chosen);
}else {
newCell.accessoryType = UITableViewCellAccessoryNone;
[self.chosen removeObject:Documents];
}
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
我知道这是由于 Cell 类型上的 dequeueReusableCellWithIdentifier
而发生的。我的问题是,一旦它们开始被重用,我可以更改什么,以便只有点击的行得到复选标记,而不是所有这些其他单元格?
最佳答案
将决定是否应检查单元格的代码移到 cellForRowAtIndexPath 中。保留对当前选定索引/项目的引用,然后在该单元格上设置复选标记。您可以将未选中的单元格设置为将辅助 View 设置为无。
关于ios - UITableView 的 AccessoryView 在多个单元格上重复使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208256/