0

I have UITableViewCell which looks like CardView. Inside cell, I should display some dynamic content. That is why I have stackview inside my cell that will have my dynamic content. But the problem is that cellForRowAt method is called every time while scrolling and stackview starts having extra elements.

How should I solve this problem? How I handled problem by myself?

I found prepareForReuse method of UITableViewCell which is called before re-configuring by cell. In that method, I clean my stackView. And now, my stackview will not have extra views. But, it is too bad for performance. The next way I tried is holding some flag inside my cell that tells me was stackview already configured. But, this approach didn't help (yes, it will not add extra elements, but content inside cells gets incorrect placement) as checking stackview length.

Here is my pseudo-code:

/// called in cellForRowAt
func configure(item: Item) {
    item.forEach {
        stackView.addArrangedSubview(ItemView(item))
    }
}

func prepareForReuse() {
    stackView.arrangedSubviews.forEach {
         $0.removeFromSuperView()
    }
}
Anjali Shah
  • 720
  • 7
  • 21
neo
  • 1,314
  • 2
  • 14
  • 34
  • How many rows will you have (it matters if it's 10 vs 50 vs 100+)? Are you filling the stack view with labels? Or are there other UI elements in the cell? It's a bit difficult to offer "optimization" suggestions in the abstract. – DonMag Jul 15 '20 at 14:45

1 Answers1

1

If the prepareForReuse and dequeuing methods leads to exceeding 0.0167 sec (60 frames per second) then maybe in your edge case it will be better to create a cell instead of dequeuing it.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    cell.configure(item: item)
    return cell
}
Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93
  • How to count how many frames per second are there? And if I create cell everytime, will it be bad for performance? – neo Jul 15 '20 at 05:46
  • Most cells designs can be dequeued or created with no significant difference in performance. How to measure frames per seconds in UIKit? Check this question: https://stackoverflow.com/questions/26888434/how-many-ways-to-calculate-the-fps-frames-per-second-of-an-ios-app-programmati – Blazej SLEBODA Jul 15 '20 at 07:22
  • But, my problem is simple. Just inside some card, I want to display dynamic content. Is every problem solved like that in iOS development? Event App Store has such a design. How would you approach my problem? – neo Jul 15 '20 at 09:05
  • dequeueing a cell and configuring it in cellForIndexPath method is the most popular, prepareForReuse is less popular. – Blazej SLEBODA Jul 15 '20 at 09:10
  • I know which one is popular. But, suppose that you should display dynamic content inside cell (which is card like). How would you approach this problem? Did you know how other developers solve problems like that? – neo Jul 15 '20 at 09:12
  • you have found solution which is prepareForReuse. It's fine, UITableViewCell has this method for a situation like yours. You say: "But, it is too bad for performance." this is a feeling or you have measured it and you see that drawing on scroll is not smooth? – Blazej SLEBODA Jul 15 '20 at 09:22
  • Yes, it is not smooth. It even seems like I notice that stackviews are removing views there. – neo Jul 15 '20 at 10:41