【问题标题】:Multiple Taps Required in UITableView for CheckmarksUITableView 中需要多次点击以进行复选标记
【发布时间】:2012-09-09 01:03:49
【问题描述】:

温柔点,我在学习。

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

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

我该如何解决这个问题?

- (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;
    }
}

【问题讨论】:

  • 当您点击单元格时是否调用了您的 didSelectRowAtIndexPath?你检查 thisCell 是否属于同一个对象?

标签: ios didselectrowatindexpath uitableview


【解决方案1】:

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

self.tableView.allowsMultipleSelection = NO;

【讨论】:

    【解决方案2】:

    有一个数组来存储选定的行索引。在-didSelect中,检查行索引是否已经存在于数组中,如果没有添加,否则从数组中删除。并重新加载表。就这样。我相信这会奏效。 :]

    【讨论】:

      【解决方案3】:

      您是否有可能将两个 UITableViewControllers 推送到导航控制器堆栈上?

      我遇到过这种情况,问题是 switch 语句中缺少“中断”,导致第二个 TableViewController 被推送。视图上的所有内容都需要点按两次,甚至是我的“返回”按钮。

      【讨论】:

        猜你喜欢
        • 2015-11-14
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 2015-12-24
        • 2011-07-19
        • 1970-01-01
        • 2023-03-27
        • 1970-01-01
        相关资源
        最近更新 更多