繁体   English   中英

在Swift中的Tableview中禁用多重行选择

[英]Disabling Mutliple Row Selection in Tableview in Swift

我是Swift的新手,我想在tableview中禁用多行选择。 我尝试实现不同类型的方法,并且下面的最终代码块不起作用。 谢谢你的帮助。

override func viewDidLoad() {
        super.viewDidLoad()
        companyTableView.allowsMultipleSelection = false
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark;
        }

        tableView.deselectRow(at: indexPath, animated: true)
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.accessoryType = .checkmark
        }

        tableView.deselectRow(at: indexPath, animated: true)
        if let cell = tableView.cellForRow(at: indexPath) {
            if selectedCells.contains(indexPath.row) {
                selectedCells.remove(indexPath.row)
                cell.accessoryType = .none
            } else {
                selectedCells.insert(indexPath.row)
                cell.accessoryType = .checkmark
            }
        }
    }

请尝试以下代码:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if !selectedCells.contains(indexPath){
            selectedCells.add(indexPath)

            if let cell = tableView.cellForRow(at: indexPath) {
                cell.accessoryType = .checkmark
            }
        }
    }


 func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

        if selectedCells.contains(indexPath){
            selectedCells.remove(indexPath)

            if let cell = tableView.cellForRow(at: indexPath) {
                cell.accessoryType = .none
            }
        }
    }

cellForRowAt使用以下代码:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if selectedCells.contains(indexPath){
                cell.accessoryType = .checkmark
        }
        else{
            cell.accessoryType = .none
        }

        return cell
    }

您的选择/取消选择通话中似乎存在问题。 使用类似这样的东西:在这里,我有一个selectedCells数组(其大小等于单元格总数的数量)

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
        if filteredStudents.count > indexPath.row {
            selectedCells[indexPath.row] = true
        }
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
        if selectedStudents.count > indexPath.row {
            selectedCells[indexPath.row] = false
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM