1
UITableViewCell's
contentViewに制約をプログラムで追加しようとすると問題が発生します。UITableViewCell:UITextViewからコンテンツビューにプログラムで制約を追加しますか?
cellForRowAt
では、プログラムでUITextView
を作成してから、制約を適用してセルのcontentViewに追加しようとしています。
しかし、イムは、クラッシュやエラー取得:私が言及したエラーに関する質問の多くを参照していますが、これについてのものを発見していない
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
// Dequeue the cell to load data
let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
if indexPath.section == 1
{
let textView: UITextView = UITextView()
textView.textColor = UIColor.black
textView.translatesAutoresizingMaskIntoConstraints = false
cell.addSubview(textView)
let leadingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.leading, multiplier: 1.0, constant: 8.0)
let trailingConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.trailing, multiplier: 1.0, constant: -8.0)
cell.contentView.addConstraint(leadingConstraint)
cell.contentView.addConstraint(trailingConstraint)
let topConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.top, multiplier: 1.0, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: cell.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: textView, attribute: NSLayoutAttribute.bottom, multiplier: 1.0, constant: 0)
cell.contentView.addConstraint(topConstraint)
cell.contentView.addConstraint(bottomConstraint)
}
return cell
}
:
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x1700951d0 UITableViewCellContentView:0x12de3e150.leading == UITextView:0x12e891400.leading + 8 (inactive)>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled.
マイ次のコードをUITableViewCell
に適用するとエラーが発生します。
誰かが正しい方向に向かうことができますか?
ありがとうございました。
馬鹿!...どのように私はいつか私は、セルの 'contentView'に制約を加えることなく、' cell' ..... :) – Pangu
yesに 'textView'を追加することを...見逃している可能性がその状況で。ハハ:D –