UITableView 复选标记重复 [英] UITableView checkmarks duplicated

查看:31
本文介绍了UITableView 复选标记重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击一行时,我一直在我的表格视图的其他部分标记复选标记.我不确定是否需要设置我的附件类型.我试过 mytableView.reloadData() 但这也没有帮助.

I keep getting checkmarks being marked in other sections of my table view when I click a row. Im not certain if I need to set my accessoryType. I tried mytableView.reloadData() however that didn't help either.

 var selected = [String]()
 var userList = [Users]()

@IBOutlet weak var myTableView: UITableView!

@IBAction func createGroup(_ sender: Any) {

    for username in self.selected{

        ref?.child("Group").childByAutoId().setValue(username)

        print(username)
    }

}
 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
    myCell.selectionStyle = UITableViewCellSelectionStyle.none
    myCell.nameLabel.text = userList[indexPath.row].name
    return myCell
}

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if myTableView.cellForRow(at: indexPath)?.accessoryType == UITableViewCellAccessoryType.checkmark{

        myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.none
       let currentUser = userList[indexPath.row]
        selected = selected.filter { $0 != currentUser.name}
    }
    else{
        myTableView.cellForRow(at: indexPath)?.accessoryType = UITableViewCellAccessoryType.checkmark
        let currentUser = userList[indexPath.row]
        selected.append(currentUser.name!)
    }
            }

推荐答案

您的问题不在此方法内,而在于加载"单元格的方法.(行单元格)

Your problem is not inside this method but in the one that "loads" the cells. (cell for row)

由于表视图使用可重用的单元格,因此它们通常会加载已经在其他地方呈现的单元格.

Since Table Views use reusable cells, more often than not they will be loading a cell that was already presented somewhere else.

因此,在单元格加载方法中,您应该重置状态"加载单元格,这包括附件类型以及您可能已更改的任何其他属性.

Because of this, on the cell loading method you should "Reset the state" the loaded cell, this includes the accessory type, and any other properties you might have changed.

所以只需在您的代码中更改此内容:

So just change this in your code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
    myCell.selectionStyle = UITableViewCellSelectionStyle.none
    myCell.nameLabel.text = userList[indexPath.row].name

    // ADD THIS
    if userList[indexPath.row].isSelected {
        myCell.accessoryType = UITableViewCellAccessoryType.checkmark
    } else {
        myCell.accessoryType = UITableViewCellAccessoryType.none
    }

    return myCell
}

"userList[indexPath.row].isSelected" 是您必须创建和管理的属性.(所以你也必须在 didSelectRowAt 方法中修改它.

"userList[indexPath.row].isSelected" is a property that YOU have to create and manage. (So you must also modify it in the didSelectRowAt method.

这篇关于UITableView 复选标记重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆