我有一个简单的问题,我没有在网上找到任何相关的东西。
什么更实际?
如果TableViewCell有10多个子视图(在stackview内部),但通常只显示3-4个,则其他视图隐藏。
或者有一个空的stackview,如果需要的话通过代码添加每个视图。
例如,对于第二个选项:

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


}

这只是一个例子,实际上这些观点更为复杂。
还有什么更实际?第一个选项比较简单,但第二个选项可能更适合内存处理。。你对此有什么看法?

最佳答案

如果TableViewCell有10多个子视图(在stackview内部),但通常只显示3-4个,其他视图则隐藏
3-4表示的是7 + -6 +是隐藏的,但仍然在内存中,所以我喜欢即时处理负载的增加/删除永久内存存在时,细胞是可见的。
同样值得一提的是,这在很大程度上取决于e.x一次可见单元格的数量如果它是全屏的,那么选项1比选项2好,因为随着可见单元格的数量增加,单元格将退出队列选项2变得更好

10-07 19:43
查看更多