滚动tableview上的重复复选标记

时间:2018-05-15 11:34:32

标签: ios swift uitableview checkbox

我有一个tableView,其中包含自定义标签和按钮(用于设置复选标记图像)。 我可以从数组中正确地获取标签上的所有文本。但是选项上的按钮(选中/未选中图像)显示复选标记但在滚动时消失并在另一个单元格上显示。 细胞正在重复使用,但我无法获得个体选择和正确的结果。 滚动tableView时会发生重复。而且数组也没有附加适当的seletecd结果。

以下是代码。

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

    let cell = tvSelectGroup.dequeueReusableCell(withIdentifier: "cellnewgroup")

    let device = userDeviceArray[(indexPath as NSIndexPath).row]
    cell?.layer.cornerRadius = 10
     cell.slider.tag = indexPath.row
     if arraySensitivity.count==0
     {
     }else
     {
        cell.slider.value = Float(arraySensitivity[indexPath.row])
      }
    if let lbl = cell?.contentView.viewWithTag(1) as? UILabel
    {
        lbl.text = device
        lbl.textColor = UIColor.white
    }

    if let btnChk = cell?.contentView.viewWithTag(2) as? UIButton
    {
        print("button...")
        btnChk.addTarget(self, action: #selector(checkboxClicked(_ :)), for: .touchUpInside)
        if btnChk.currentImage==#imageLiteral(resourceName: "Checkmark")
        {
            addDeviceInArray(cell: cell!)
        }
    }
    print("arr\(arr)")
    return cell!
}

@objc func checkboxClicked(_ sender: UIButton)
{
    print("checked..")
    sender.isSelected = !sender.isSelected
}

@objc func addDeviceInArray(cell:UITableViewCell)
{
    let lbl = cell.contentView.viewWithTag(1) as? UILabel
    if !arr.contains((lbl?.text)!)
    {
        arr.append((lbl?.text)!)
        print("array in label\(arr)")
    }
}

2 个答案:

答案 0 :(得分:0)

  1. 创建状态数组 - false / true。基于单元格或任何内容项目的更改状态。
  2. 创建自定义单元格
  3. 按下按钮
  4. 在单元格内添加处理程序

    例如:

    var states: [Bool] = [true, true, false]
    var deviceName[String] = ["PC", "Mac", "iPhone"]
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellnewgroup") as? MyCell
    
    let device = deviceName[indexPath.row]
    cell?.layer.cornerRadius = 10
    
    if state[indexPath.row] {
       cell.button.setImage("uncheckmark")
    
    } else {
      cell.button.setImage("checkmark")
    }
    
    
    cell.onClick = {
    
      state[indexPath.row] != state[indexPath.row]
    
    }
    
    if states[indexPath.row] {
        lbl.text = device
        lbl.textColor = UIColor.white
    } else  { // remove this
        // print("button...")
    
    // add button to cell class and use it.
       //print("arr\(arr)")
       //return cell!
    }
    
    
    class MyCell: UITableViewCell {
    
        @IBOutlet var button: UIButton // connect to storyboard or create your own
        var onClick: () -> Void = {}
    
    
    
        // add this method to your cell, connect to storyboard or create your own target
        @IBAction func checkboxClicked(sender: UIButton) {
    
             onClick()
    
        }
    }
    

答案 1 :(得分:0)

单击checkboxClicked按钮,在userDeviceArray中管理标志。最初为数组的所有对象设置0,如果已经选中了单击的行值,则按钮单击将其设置为0,否则将其设置为1&重新加载特定的表格单元格。并在表格单元格中添加带有数组对象标志值的检查。

我希望这会有所帮助。