flolopi
flolopi

Reputation: 43

every cell of my tableview reloads when add-cell button is clicked

enter image description here picture of two cells. the first one is with the enabled button and the filled label

enter image description here picture of the tableview after i have added 3 new files. every cell is reset

hey guys, i have created a tableview and an addButton. when i click the addButton a new custom xib Cell is loaded. Every cell has a button (checkbox button that changes the image if it is clicked). when i click the button in the cell a new String should pop up in the label in the cell. if the addButton is clicked after this process everything disappears in the cell. the label is empty again and the button is unchecked. Now i want to know how i can stop the reload for every cell if the addButton is clicked.

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

    }

Upvotes: 2

Views: 792

Answers (1)

vadian
vadian

Reputation: 285082

You can insert a single row

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

Side note: If reloading the entire table view changes the UI in the other cells then there’s something wrong with your design.

Upvotes: 3

Related Questions