【问题标题】:Swift 3: Why cant I change cell.accessoryType and cell.textLabel in didDeselectRowAtSwift 3:为什么我不能在 didDeselectRowAt 中更改 cell.accessoryType 和 cell.textLabel
【发布时间】:2016-11-30 22:05:54
【问题描述】:

我不明白为什么我不能切换选定的表格行。 我在 didSelectRowAt 中成功设置了附件类型。 但我似乎无法在 didDeselectRowAt 中将 accessoryType 设置为 none。

我的数据:

serviceItems = ["Heater", "Air Conditioner", "Boiler", "Heat Pump"]

这是我的 tableview 覆盖:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryType = .checkmark
    let serviceItem = serviceItems[indexPath.row] as! String
    cell.textLabel?.text = serviceItem
}

// does not remove checkmark
// does not change textLabel to "test" 
// DOES print "DESELECTING" (so I know its getting called)
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    cell.accessoryType = .none
    cell.textLabel?.text = "test"
    print("DESELECTING")
}

【问题讨论】:

标签: ios swift uitableview swift3 didselectrowatindexpath


【解决方案1】:

应该一直使用tableView.cellForRow 而不是tableView.dequeueReusableCell

改变:

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

收件人:

let cell = tableView.cellForRow(at: indexPath)

【讨论】:

    【解决方案2】:

    我把所有东西都放在了 DidSelectRow 中,请看这里:

    func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath)
    {
        if let cell = tableView.cellForRow(at: indexPath) {
            if self.selectedIndexPaths.contains(indexPath)
            {
                // Config Cell
                cell.accessoryType = .none
                self.selectedIndexPaths.remove(indexPath)
            }
            else
            {
                // Config Cell
                cell.accessoryType = .checkmark                    
                self.selectedIndexPaths.add(indexPath)
            }
            // anything else you wanna do every time a cell is tapped
        }    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 2017-09-20
      • 2012-10-17
      • 2016-03-10
      相关资源
      最近更新 更多