我有一个简单的问题,我没有在网上找到任何相关内容。
什么比较实用?
- 有一个 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/