UITableViewCell - 隐藏未使用的视图或什至不添加它们?

UITableViewCell - hiding unused views or not even adding them?

我有一个简单的问题,我在网上找不到任何相关内容。


哪个比较实用?

例如第二个选项:

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({ [=10=].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


}

这只是一个例子,实际上这些观点更复杂。


什么比较实用?第一个选项更容易,但第二个选项可能更适合内存处理。您对此有何看法?

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 表示 7+ - 6+ 被隐藏但仍在内存中,所以我更喜欢 Add/Remove 的即时处理负载而不是单元格可见时的永久内存存在

还值得一提的是,对于 e.x,这在很大程度上取决于一次可见单元格的数量,如果是全屏,则选项 1 优于选项 2,因为随着可见单元格数量的增加,单元格会出列选项2 变得更好