选中自定义tableView单元格和节标题

时间:2020-03-23 14:52:12

标签: ios swift uitableview

在点击自定义节标题时,是否可以允许tableView自动选中该节中的所有自定义行单元格?我还没有看过任何有关此内容的教程,并且想知道是否有可能?我将如何跟踪每一行和每一节,以便可以修改具有与表中的行数相同数量的元素的字符串数组中的字符串值?

1 个答案:

答案 0 :(得分:0)

在这种情况下(下面),我只是在单击的单元格上放置了一个附件类型,然后将所选元素附加到所选项目列表中。您可以使用手势处理在按下某些按钮时附加列表的所有元素。

这是我的代码:

// MARK: UITableViewDelegate

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    guard let cell = tableView.cellForRow(at: indexPath) else { return }

    if cell.accessoryType == .none {
        cell.accessoryType = .checkmark
        selectedItems.append(items[indexPath.row])
    } else {
        cell.accessoryType = .none
        if let text = cell.textLabel?.text,
            let index = selectedItems.firstIndex(where: { $0.name == text}) {
            selectedItems.remove(at: index)
        } else {
            print("Error removing selected item from array.")
        }
    }
}

希望这会有所帮助。问候!