Ios UITableViewCell-隐藏未使用的视图,甚至不添加它们?
我有一个简单的问题,我在网上没有找到任何相关的 什么更实际 如果TableViewCell在stackview中有10多个子视图,但通常仅显示3-4个,则其他子视图将隐藏。 或者拥有一个空的stackview,并根据需要通过代码添加每个视图。 例如,对于第二个选项:Ios UITableViewCell-隐藏未使用的视图,甚至不添加它们?,ios,swift,uitableview,stackview,Ios,Swift,Uitableview,Stackview,我有一个简单的问题,我在网上没有找到任何相关的 什么更实际 如果TableViewCell在stackview中有10多个子视图,但通常仅显示3-4个,则其他子视图将隐藏。 或者拥有一个空的stackview,并根据需要通过代码添加每个视图。 例如,对于第二个选项: func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { var cell :
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在stackview中有10多个子视图,但通常只显示3-4个,则其他子视图将隐藏
3-4表示7+-6+是隐藏的,但仍在内存中,所以我更喜欢添加/删除的即时处理负载,而不是当单元格可见时永久内存的存在
同样值得一提的是,这在很大程度上取决于e.x一次可见单元格的数量。如果是全屏显示,则选项1优于选项2,因为随着可见单元格数量的增加,单元格会退出队列。选项2变得更加合理!我的应用程序在屏幕上只有2-3个单元格,但我仍然认为我将坚持第二种方法。谢谢你的回答!