2016-09-11 15 views
2

私はInstagramのコメントテーブルビューを見ています。各セルのサイズは、コメントの長さによって異なります。上下にいくつかのパディングがあります。今私はテーブルビューセルのセルフサイジングの問題を除いて、同様のことをやってみました。パディング効果を達成するために制約を追加しようとしますが、テキストは次のセルと重なります。自己サイジングセルを持つUITableViewCell内にパディングを追加するにはどうすればよいですか?

私はtableView.contentInsetを試しましたが、何も変更されませんでした。

は、ここで私が欲しいものです:

enter image description here

はここで起こって終わるものです:

enter image description here

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) 
} 

答えて

1

あなたの制約が私には正しく見えませんでした。 contentViewの境界は、ラベルのより大きいのようにして、右と下の制約のために負の値を設定する必要があります。

commentLabel.leftAnchor.constraintEqualToAnchor(contentView.leftAnchor, constant: 10).active = true 
commentLabel.rightAnchor.constraintEqualToAnchor(contentView.rightAnchor, constant: -10).active = true 
commentLabel.topAnchor.constraintEqualToAnchor(contentView.topAnchor, constant: 10).active = true 
commentLabel.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor, constant: -10).active = true 

あなたがのためにヘルパー関数を定義することができます。

commentLabel.rightAnchor.constraintEqualToAnchor(contentView.rightAnchor, constant: -10).active = true 

ここにあなたのコードの修正版です以下のように後で使用します。

func inset(view: UIView, inset: UIEdgeInsets) { 
    if let superview = view.superview { 
    view.translatesAutoresizingMaskIntoConstraints = false 

    view.leftAnchor.constraintEqualToAnchor(superview.leftAnchor, constant: inset.left).active = true 
    view.rightAnchor.constraintEqualToAnchor(superview.rightAnchor, constant: -inset.right).active = true 
    view.topAnchor.constraintEqualToAnchor(superview.topAnchor, constant: inset.top).active = true 
    view.bottomAnchor.constraintEqualToAnchor(superview.bottomAnchor, constant: -inset.bottom).active = true 
    } 
} 

-

inset(commentLabel, inset: UIEdgeInsetsMake(10, 10, 10, 10)) 
関連する問題