简体   繁体   English

如何在具有自调整大小的单元格的 UITableViewCell 中添加填充?

[英]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.

相关问题 自调整UITableViewCell的内部带有UITableView的功能-自调整不起作用 - Self-sizing UITableViewCell with UITableView inside it - self-sizing doesn't work Xamarin iOS如何在编辑时制作自定尺寸的UITableViewCell - Xamarin ios how to make a self-sizing UITableViewCell while editing 如何使水平自定义UICollectionView单元居中? - How to center horizontally self-sizing UICollectionView Cells? 如何为自定义大小的单元格正确设置自动布局约束 - How to setup autolayout constraints properly for self-sizing cells 没有自动布局的自调整 UITableViewCell - Self-sizing UITableViewCell without Auto Layout iOS:在UITableViewCell中创建自调整大小的UITextView? - IOS:Creating a Self-Sizing UITextView Within a UITableViewCell? UITableViewCell 扩展 animation 与自定尺寸标签,可视 animation 问题 - UITableViewCell expanding animation with self-sizing labels, visual animation problem 自定义UITableViewCell永远不会以AutoLayout自调整大小显示 - Custom UITableViewCell never showing up with AutoLayout self-sizing 如何开发一个自定义的UICollectionViewLayout,它具有与自定义单元格交错的列? - how to develop a custom UICollectionViewLayout that has staggered columns with self-sizing cells? iOS AutoLayout可自动调整整个表格的高度(不是单元格!) - iOS AutoLayout self-sizing whole table (not cells!) height
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM