【问题标题】:UITableViewCell - hiding unused views or not even adding them?UITableViewCell - 隐藏未使用的视图甚至不添加它们?
【发布时间】:2019-05-29 21:25:42
【问题描述】:

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


什么比较实用?

  • 有一个 TableViewCell,其中包含超过 10 个子视图(在堆栈视图内),但通常只显示 3-4 个,其他则隐藏。
  • 或者有一个空的堆栈视图,如果需要的话,可以通过代码添加每个视图。

例如第二个选项:

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


}

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


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

【问题讨论】:

    标签: ios swift uitableview stackview


    【解决方案1】:

    有一个 TableViewCell 有超过 10 多个子视图(在 stackview 内部),但通常只显示 3-4 个,其他的被隐藏

    显示的 3-4 意味着 7+ - 6+ 被隐藏但仍在内存中,因此我更喜欢添加/删除的即时处理负载,而不是当单元格可见时永久内存存在

    值得一提的是,这在很大程度上取决于一次可见单元格的数量,例如,如果它是全屏的,那么选项 1 比 2 更好,因为随着可见单元格数量的增加,单元格出列选项 2 变得更好

    p>

    【讨论】:

    • 合理!我的应用程序在屏幕上只有 2-3 个单元格可见,但我仍然认为我会坚持使用第二种方法。谢谢你的回答!
    猜你喜欢
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    相关资源
    最近更新 更多