So I'm having a problem with updating the height of a UITableViewCell
when the text of its label gets changed.
I'm not good at explaining thing, therefore I've created a repo on Github so that you can just grab and run. The problem should show up when you scroll up and down the table view (to make the cell reused).
What I did was :
UILabel
in it. RxSwift
to register the changes in the view model and update the text of the UITable
accordingly. UITableViewController
) to recalculate its height. tableView.beginUpdates
and tableView.endUpdates
to update the height of the cell. Results :
What I tried :
tableView.reloadData()
. -> Infinite Loop tableView.reloadRows()
-> This works but I don't know why. What I want to achieve:
Some code snippets: (Although it'd better to go check the repo out.):
Cell register view model
override func awakeFromNib() {
super.awakeFromNib()
viewModel.title
.observeOn(MainScheduler.instance)
.subscribe(onNext: { [weak self] title in
guard let cell = self else { return }
cell.label.text = title
cell.delegate?.requestSizeRecalculate(cell: cell)
})
.disposed(by: disposeBag)
}
Delegate methods in UITableViewController
func requestSizeRecalculate(cell: SimpleCell) {
print("Size recalculating....")
method1(cell: cell)
//method2(cell: cell)
//method3(cell: cell)
}
private func method1(cell: SimpleCell) {
// Will not cause infinite loop but will have duplicates.
cell.setNeedsUpdateConstraints()
tableView.beginUpdates()
tableView.endUpdates()
}
private func method2(cell: SimpleCell) {
// Will cause infinite loop
tableView.reloadData()
}
private func method3(cell: SimpleCell) {
// This method works pretty well.
if let indexPath = tableView.indexPath(for: cell) {
tableView.reloadRows(at: [indexPath], with: .none)
}
}
It's my first time asking questions on Stackoverflow. I know I might have made things more complicated than it should be. But if anyone can offer any suggestions based on your experience, I would appreciate very much!
You can do:
tableView.beginUpdates()
tableView.reloadRows(at: [indexPathOfCell], with: .none)
tableView.endUpdates()
I have seen your code. Now you don't need to Calculate Height of row. when you give just top , leading , bottom and Trailing constrain. Xcode will calculate height of label and increase cell size According to label height. you need to pass just 1 method.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
you have done all the things correctly. you need to just write above method. Don't required this method now.
extension ViewController: SimpleCellDelegate {
func requestSizeRecalculate(cell: SimpleCell) {
print("Size recalculating....")
I have uploaded your project on this link with changes. Added 1 more large text in array to test self sizing.
Here is Screenshot of Out:-
I hope this answer is helpful for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.