ios - UITableView 中需要多次点击才能显示复选标记

标签 ios didselectrowatindexpath uitableview

温柔一点,我正在学习。

下面的代码实际上工作得很好,除了在我第一次点击一行(适本地放置了一个复选标记)之后,我必须点击一个单元格两次才能让它删除相同的复选标记。

同样,删除后,需要点击两次才能将复选标记放回去。

如何解决这个问题?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];
    NSInteger selectedRow = indexPath.row;

    if (thisCell.accessoryType == UITableViewCellAccessoryNone) {
        thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
        if (selectedRow == 0) dm = 1;
        else if (selectedRow == 1) athero1 = 1;
        else if (selectedRow == 2) athero2 = 1;
        else if (selectedRow == 3) athero3 = 1;
        else if (selectedRow == 4) familyHistory1 = 1;
        else if (selectedRow == 5) familyHistory2 = 1;
    }
    else  {
        thisCell.accessoryType = UITableViewCellAccessoryNone;
        if (selectedRow == 0) dm = 0;
        else if (selectedRow == 1) athero1 = 0;
        else if (selectedRow == 2) athero2 = 0;
        else if (selectedRow == 3) athero3 = 0;
        else if (selectedRow == 4) familyHistory1 = 0;
        else if (selectedRow == 5) familyHistory2 = 0;
    }
}

最佳答案

确保allowsMultipleSelection未设置为YES。不使用此属性时设置的默认值为NO。如果您在类中的任何位置使用此属性,请尝试将其设置为 NO 以检查它是否有效。这将导致需要点击 2 次才能选择一行。

self.tableView.allowsMultipleSelection = NO;

关于ios - UITableView 中需要多次点击才能显示复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335728/

相关文章:

ios - 将 RGBA 数组转换为 UIImage 时出现错误结果

objective-c - 在具有滚动索引的 UITableView 中延迟加载图像

ios - 如何在 iOS、Objective C 中的 TableView 的每个部分中仅实现单个单元格选择

ios - 当前索引与错误数据关联

ios - 如何调整 UIView 的横向方向?

ios - 在展开 UITableVewCell 自定义单元格类的 Optional 值时意外发现 nil

iphone - iOS 中的 NSString 值验证

ios - 有没有办法使用 swift 始终在 iOS 中将 Google map 标记固定到其 map 的中心?

ios - UISegmentedControl 的 NSAttributedstring

ios - swift didSelectRowAtIndexPath 可以多次调用