代码之家  ›  专栏  ›  技术社区  ›  jamesMcKey

UITableView在滚动时会丢失与所选单元格关联的属性。如何保存这些属性?-斯威夫特

  •  0
  • jamesMcKey  · 技术社区  · 6 年前

    我有一个带有自定义弹出窗口的应用程序,其中包含一个表视图(见下文)。当我点击复选框(ui按钮)时,我交换背景图像,并在选中和未选中之间切换。当我有一个复选框时,当我向上或向下滚动时,复选框默认回到unchecked。有人能告诉我如何在滚动时保持每个单元格的选中状态吗?

    enter image description here

     class CustomHashTagPopup: UIViewController, UITableViewDelegate, UITableViewDataSource{
    
    
    // CREATE TABLEVIEW CELLS
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        let cell = self.tableViewPicker.dequeueReusableCell(withIdentifier: "pickerCell") as! PreviewTableViewCustomPickerCell
    
        if(indexPath == [0,0]){
            let image = UIImage.init(named: "add")
            cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
            cell.pickerLabel.text = "Create New HashTag"
            cell.isCreateTagCell = true
    
        }else{
            let image = UIImage.init(named: "uncheck")
            cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
            cell.pickerLabel.text = arrayHashTags[indexPath.row]
            cell.isCreateTagCell = false
        }
        return cell
    
    }
    
    
     }
    
    
     class PreviewTableViewCustomPickerCell: UITableViewCell {
    
     var isSelect: Bool = false
    var isCreateTagCell: Bool = false // TO DISTINGUISH 'CEATE NEW HASHTAG OPTION'
    
    
    
    /*** OUTLETS ***/
    
    @IBOutlet weak var checkBoxOutlet: UIButton!
    @IBOutlet weak var pickerLabel: UILabel!
    
    
     // TAP ON CHECK BOX
    @IBAction func checkBoxBtn(_ sender: Any) {
    
    
        if(isSelect == false && isCreateTagCell == false){
            isSelect = true
            // SHOW GREEN SELECTED CHECK BOX
            let image = UIImage.init(named: "tick")
            self.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
    
        }else if(isSelect == true && isCreateTagCell == false){
            isSelect = false
            // SHOW UNCHECKED BOX
            let image = UIImage.init(named: "uncheck")
            self.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
        }
    }
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Shehata Gamal    6 年前

    cellForRowAt ,您可以为里面的按钮将操作委托给vc

    let arr = [false,true,false,false,false] first index is dummy as it will be skipped in Create New HashTag
    

    //

    @objc func btnClicked(_ sender:UIButton) {
       arr[sender.tag] = !arr[sender.tag]
        // reload indexPath of row = sender.tag , section = 0
    }
    

    //

    内部 cellForRowAt公司

    if (indexPath == [0,0]) {
        let image = UIImage(named: "add")
        cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
        cell.pickerLabel.text = "Create New HashTag"
        cell.isCreateTagCell = true
    
    }else{
        let image = UIImage(named: arr[indexPath.row] ? "tick" : "uncheck") // edited here 
        cell.checkBoxOutlet.setBackgroundImage(image!, for: .normal)
        cell.pickerLabel.text = arrayHashTags[indexPath.row]
        cell.isCreateTagCell = false
    }
    
    cell.button.addTarget(self, action: #selector(btnClicked(_:)), for: .touchUpInside)
    cell.button.tag = indexPath.row