【发布时间】: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