[英]How to add padding inside a UITableViewCell with self-sizing cells?
I'm looking at the Instagram comment table view, and each cell self sizes depending on the length of the comment with some kind of padding on the top and bottom.我正在查看 Instagram 评论表视图,每个单元格的大小取决于评论的长度,顶部和底部有某种填充。 Now I tried doing something similar, except I have a problem self-sizing the table view cell.
现在我尝试做类似的事情,但我在调整表格视图单元格的大小时遇到了问题。 I try to add constraints to achieve the padding effect, but the text overlaps the next cell.
我尝试添加约束以实现填充效果,但文本与下一个单元格重叠。
I've tried tableView.contentInset
but it didn't change anything.我试过
tableView.contentInset
但它没有改变任何东西。
Here's what I want:这是我想要的:
Here's what ends up happening:这是最终发生的事情:
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 130.0
tableView.tableFooterView = UIView()
tableView.separatorInset.left = 50
tableView.registerClass(CommentCellView.self, forCellReuseIdentifier: cellid)
tableView.rowHeight = UITableViewAutomaticDimension
tableView.contentInset = UIEdgeInsetsMake(15, 15, 15, 15)
}
override func viewDidAppear(animated: Bool) {
tableView.reloadData()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellid, forIndexPath: indexPath) as! CommentCellView
cell.layoutIfNeeded()
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 60.0
}
}
class CommentCellView: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: .Subtitle, reuseIdentifier: reuseIdentifier)
contentView.addSubview(commentLabel)
commentLabel.leftAnchor.constraintEqualToAnchor(contentView.leftAnchor).active = true
commentLabel.rightAnchor.constraintEqualToAnchor(contentView.rightAnchor).active = true
commentLabel.topAnchor.constraintEqualToAnchor(contentView.topAnchor, constant: 10).active = true
commentLabel.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor, constant: 10).active = true
self.contentView.layoutMargins = UIEdgeInsetsMake(15, 15, 15, 15)
}
Your constraints didn't look correct to me.你的约束在我看来不正确。 You should set a negative value for right and bottom constraints as contentView's bounds are greater than the label's:
您应该为右侧和底部约束设置负值,因为 contentView 的边界大于标签的边界:
commentLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10).isActive = true
Here is the corrected version of your code:这是您的代码的更正版本:
commentLabel.leftAnchor.constraint(equalTo: contentView.leftAnchor, constant: 10).isActive = true
commentLabel.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant: -10).isActive = true
commentLabel.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 10).isActive = true
commentLabel.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -10).isActive = true
You can define a helper function for later use like follows:您可以定义一个辅助函数供以后使用,如下所示:
func inset(view: UIView, insets: UIEdgeInsets) {
if let superview = view.superview {
view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: superview.leftAnchor, constant: insets.left).isActive = true
view.rightAnchor.constraint(equalTo: superview.rightAnchor, constant: -insets.right).isActive = true
view.topAnchor.constraint(equalTo: superview.topAnchor, constant: insets.top).isActive = true
view.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: -insets.bottom).isActive = true
}
}
-- ——
inset(commentLabel, insets: UIEdgeInsetsMake(10, 10, 10, 10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.