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

标签 ios swift uitableview stackview

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


什么比较实用?

  • 有一个 TableViewCell,它有超过 10 个 subview (在 stackview 内部),但通常只显示 3-4 个,其他的被隐藏。
  • 或者有一个空的堆栈 View ,并在需要时通过代码添加每个 View 。

例如第二个选项:

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


}

这只是一个示例,实际上这些 View 更为复杂。


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

最佳答案

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

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

关于ios - UITableViewCell - 隐藏未使用的 View 或什至不添加它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56368745/

相关文章:

iOS 8 UISearchController 的 searchBar 重叠 tableVIew

html - 当用户单击 “add to home screen” 时,我可以向我的 iPhone 目标 Web 应用程序添加其他内容吗?

objective-c - 圆弧 : Memory does not get reclaimed?

ios - 无法在模拟器 : Xcode 6. 0.1 iOS 8 中运行应用程序

IOS触发位置权限弹窗

ios - 使 UITableView 中的最后一行出现在顶部

ios - 以单一名称将多个对象保存到核心数据

image - ios Objective C 请帮忙处理数组!

iOS 11 AVPlayerViewController 禁用捏合/拖动手势

ios - 在我的应用程序中支持 iPad 和 iPhone 的最佳方法是什么?