ios - UITableView 的 AccessoryView 在多个单元格上重复使用

标签 ios objective-c uitableview accessoryview

我有一个 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/

相关文章:

ios - Facebook iOS SDK 'one-time' 授权

objective-c - 在 Objective-C 中,未赋值的 int 变量是 nil 吗?

iphone - 如何更改突出显示状态下的 UITableViewCell 文本颜色?

swift - tableViewCells 中的 collectionViews 之间的通信。这个tableView在另一个tableViewCell中

objective-c - UITableViewCell 中的 UIGestureRecognizer

ios - DisclosureIndicator 不检测触摸

iOS 更新后保留旧的启动屏幕

ios - 比较ios中的两个时间值?

ios - iOS-处理多个复选框

ios - navigationController.topViewController 与 Storyboard