What is the correct way to use prepareForReuse?

后端 未结 2 843
日久生厌
日久生厌 2020-12-02 23:57

Need help with understanding how to use prepareForReuse() in UIKit. The documentation says

you should only reset attributes of the cell that are not

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 00:33

    As stated by the documentation you only have to use the said method to reset attributes that are not related to the content. As for reseting the text/number of lines.... of your labels you could do it from within tableView(_:cellForRowAt:) just before you set their new value, like so:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        cell.label.text = "" //reseting the text
        cell.label.text = "New value"
        return cell
        }
    

    OR

    you could take a more object oriented approach and create a subclass of UITableViewCell and define a method say configureCell() to deal with all the resetting and value setting of newly dequeued cells.

提交回复
热议问题