2
私はInstagramのコメントテーブルビューを見ています。各セルのサイズは、コメントの長さによって異なります。上下にいくつかのパディングがあります。今私はテーブルビューセルのセルフサイジングの問題を除いて、同様のことをやってみました。パディング効果を達成するために制約を追加しようとしますが、テキストは次のセルと重なります。自己サイジングセルを持つUITableViewCell内にパディングを追加するにはどうすればよいですか?
私はtableView.contentInset
を試しましたが、何も変更されませんでした。
は、ここで私が欲しいものです:
はここで起こって終わるものです:
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)
}