Swift: UITableView 单元格受到另一个部分中单元格的影响

Swift: UITableView cells being affected by cells in another section

我有一个功能,可以将 第 0 节 中单元格的 accessoryType 更改为复选标记。它工作得很好,并在选中时显示复选标记,但如果选择了 第 1 部分第 2 部分 中的单元格,它将移动复选标记第 0 节。如果有人能告诉我如何解决这个问题,那就太好了。

    //Adds Checkmark to Cells when Selected
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let section = 0
        let numberOfRows = tableView.numberOfRowsInSection(section)
        for row in 0..<numberOfRows {
            if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: row, inSection: section)) {
                cell.accessoryType = row == indexPath.row ? .Checkmark : .None
            }
        }
    }

由于您的单元格是重复使用的,如果您在创建单元格时没有设置它们,就会发生这种情况。

您需要在 cellForRowAtIndexPath 功能覆盖中重新设置您的单元格 accessoryType