单击“添加单元格”按钮时,tableview的每个单元格都会重新加载

时间:2019-09-05 12:38:32

标签: swift tableview cell reload

enter image description here 两个单元格的图片。第一个是启用按钮和填充标签

enter image description here 我添加了3个新文件后,tableview的图片。每个单元格都重置

伙计们,我创建了一个tableview和一个addButton。当我单击addButton时,将加载一个新的自定义xib单元。每个单元格都有一个按钮(如果单击该按钮,复选框将更改图像)。当我单击单元格中的按钮时,新的字符串应在单元格的标签中弹出。如果在此过程之后单击addButton,则所有内容都将在单元格中消失。标签再次为空,并且未选中该按钮。现在,我想知道如果单击addButton,如何停止每个单元的重新加载。

var sliderValues = [String]()

    @IBAction func addNewCell(_ sender: Any) {
        sliderValues.insert("\(Int(waterAmountSlider.value)) L", at: 0)
        tableView.reloadData()


    }

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sliderValues.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "xibcell", for: indexPath) as! TableViewCell


        cell.cellButton.setImage(UIImage(named: "disabledCheckbox"),    for: UIControl.State.normal)
        cell.secondLabel.text = sliderValues[indexPath.row]
        cell.firstLabel.text = ""
        cell.firstLabel.font = UIFont(name: "Gill Sans", size: 19)
        cell.secondLabel.font = UIFont(name: "Gill Sans", size: 19)


        return cell

    }

1 个答案:

答案 0 :(得分:3)

您可以插入一行

@IBAction func addNewCell(_ sender: Any) {
    sliderValues.insert("\(Int(waterAmountSlider.value)) L", at: 0)
    tableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}

侧面说明:如果重新加载整个表格视图会更改其他单元格中的UI,则说明您的设计有问题。