首页 > 解决方案 > 如何在 [Int : Bool] 中获取 Int 的值?

问题描述

我试图从中获取 Int 的值,[Int : Bool]以便我可以从 tableView 中删除行值。

这是我的代码。

 var checkmarks = [Int : Bool]()

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedNamesIndex = indexPath.row
    let cell = tableView.cellForRow(at: indexPath)

    if let index = selectedNamesIndex {
        if (cell?.accessoryType == .checkmark) {
            cell!.accessoryType = .none
            checkmarks[indexPath.row] = false
            hasChecked = false

            let indexPathTapped = tableView.indexPath(for: cell!)
            let name = namelist[(indexPathTapped!.row)]
            print(name, hasChecked)
            selectedNamesToBroadcast.remove(at: checkmarks) // theres an error here: Cannot convert value of type '[Int : Bool]' to expected argument type 'Int'
        } else {
            cell!.accessoryType = .checkmark
            checkmarks[indexPath.row] = true
            hasChecked = true

            let indexPathTapped = tableView.indexPath(for: cell!)
            let name = namelist[(indexPathTapped!.row)]
            print(name, hasChecked)
            selectedNamesToBroadcast.append(name)

            let selectedNamesToBroadcast = name

        }
    }

我希望能够取消选中颜色。这样当我更新数据库时,它将被删除。

标签: arraysuitableviewcheckmark

解决方案


你可能想要

if let index = selectedNamesToBroadcast.index(of: name) {
    selectedNamesToBroadcast.remove(at: index)
}

推荐阅读