【问题标题】:UITableView checkmarks duplicatedUITableView 复选标记重复
【发布时间】:2017-06-29 00:25:02
【问题描述】:

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

 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!)
    }
            }

【问题讨论】:

    标签: ios swift uitableview didselectrowatindexpath


    【解决方案1】:

    您的问题不在此方法中,而是在“加载”单元格的方法中。 (行的单元格)

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

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

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

    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 方法中进行修改。

    【讨论】:

    • 这有帮助,但是现在在选择了一行之后,我向下滚动并备份 tableView 不再检查所选行。
    • 您应该“保存”选中单元格的状态。所以你的数据源也必须有这些信息。类似于:userList[indexPath.row].isSelected
    • 我建议您使用Set<NSIndexPath> 来存储您选择的索引路径。
    • 我在“if userList[indexPath.row].isSelected”这一行出现错误,说明用户类型的值没有成员“.isSelected”
    • 因为它只是一个例子......你必须以某种方式实现它,一个外部数组,或者 UserList 类中的一个属性,或者其他东西。您的代码似乎有“选定”数组,所以只需使用它,例如检查选定数组是否包含值“userList [indexPath.row].name”。如果它确实将其标记为选中。或者像 Paulw11 提到的那样直接存储索引路径。
    【解决方案2】:

    问题是您没有正确维护选定的用户信息,这些信息将在您滚动表格和单元格必须重新加载数据时使用。

    由于您已经创建了 var selected = [String]() ,我建议使用相同的。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let myCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell
        let dataFoundInselectedArr =  selected.filter { $0 == userList[indexPath.row].name}
    
        if(dataFoundInselectedArr.count > 0){
              myCell.accessoryType = UITableViewCellSelectionStyle.checkmark
        }else{
              myCell.accessoryType = UITableViewCellSelectionStyle.none
        }
    
        myCell.nameLabel.text = userList[indexPath.row].name
        return myCell
    }
    

    表格选择委托方法保持不变。

    【讨论】:

    • for myCell.selectionStyle = UITableViewCellSelectionStyle.checkmark。没有成员复选标记。我尝试将其更改为默认值,但复选标记仍在重复
    • 检查我更新的答案或简单地使用图像视图并根据上述条件设置复选框图像。
    • 将其更改为 myCell.accessoryType = UITableViewCellSelectionStyle.checkmark 也会带来相同的错误“没有成员选中标记”,现在 myCell.accessoryType = UITableViewCellSelectionStyle.none 也会出现相同的错误
    • 我建议使用imageview来代替checkbox_deselect和checkbox_selected图像的两个图像,并根据当前逻辑设置图像。
    猜你喜欢
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多