0

I have a simple question for which I haven't found anything related online.


What is more practical?

  • Having a TableViewCell that has more than 10+ subviews (inside of a stackview), but generally only 3-4 is shown, others are hidden.
  • Or having an empty stackview, and adding every view by code if needed.

For example for the second option:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell : PostCell

    guard let post = PostsStore.shared.posts.getPost(index: indexPath.row) else {
        return UITableViewCell()
    }



    // Remove reused cell's stackview's views
    cell.stackView.subviews.forEach({ $0.removeFromSuperview() })

    if post.voteAddon != nil {

        if let voteView = Bundle.main.loadNibNamed("VoteView", owner: self, options: nil)?.first as? VoteView {
            voteView.voteForLabel = "Vote for X or Y"

            voteView.buttonX.setTitle("\(post.votes.x) votes", for: .normal)
            voteView.buttonY.setTitle("\(post.votes.y) votes", for: .normal)

            cell.stackView.addArrangedSubview(voteView)
        }
    }
    if post.attachments != nil {

        if let attachmentsView = Bundle.main.loadNibNamed("AttachmentsView", owner: self, options: nil)?.first as? AttachmentsView {
            attachmentsView.attachmentImageView.image = UIImage()


            cell.stackView.addArrangedSubview(attachmentsView)
        }
    }


    cell.delegate = self


    return cell


}

This is just an example, in reality those views are more complex.


What would be more practical? The first option is easier, but the second might be better for memory handling.. What are your thoughts about this?

Kárpáti András
  • 1,221
  • 1
  • 16
  • 35

1 Answers1

1

Having a TableViewCell that has more than 10+ subviews (inside of a stackview), but generally only 3-4 is shown, others are hidden

3-4 shown means that 7+ - 6+ are hidden but still in memory , so i prefer instant processing load of the Add/Remove to the permanent memory existence when the cell is visible

It's also worth saying that this heavily depend on the number of visible cells at a time for e.x if it's full screen then option 1 is better than 2 because cells are dequeued as number of visible cells increase option 2 becomes more better

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Reasonable! My application has only 2-3 cells visible on the screen, but still I think I'm going to stick with the second method. Thank you for the answer! – Kárpáti András May 31 '19 at 07:48