如何使用Swift将多个单元格选择转换为单个单元格选择

问题描述 投票:0回答:1

在我的场景中,我试图一次创建单个单元格选择checkmark。我在下面的代码中使用isSelected Bool值对多个单元格进行了选择,以选择单元格。现在,如何为单个单元格selection转换以下代码。

下面的我的代码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")!
    let item = self.titleData[indexPath.row]
    cell.textLabel?.text = item.title
    cell.accessoryType = item.isSelected ? .checkmark : .none
    return cell
}
ios swift tableview
1个回答
0
投票

您可以为此使用didSelectRowAtdidDeselectRowAt。一次只能启用一个选择。

// assign isSelected true and accessoryType to checkmark

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    self.titleData[indexPath.row].isSelected = true
    cell.accessoryType = .checkmark

}

// assign isSelected false and accessoryType to none

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath)
    self.titleData[indexPath.row].isSelected = false
    cell.accessoryType = .none
}
© www.soinside.com 2019 - 2024. All rights reserved.