Swift: UITableView单元格被另一个section中的单元格影响



我有一个函数,将Section 0中的单元格的accessoryType更改为复选标记。它工作得很好,当被选中时显示一个复选标记,但是如果Section 1Section 2中的单元格被选中,它将移动Section 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 function override中设置单元格的accessoryType

最新更新