2017-09-08 13 views
1

UITableViewのUITableViewCellの動的高さに答える質問がたくさんあります。しかし、私は私の問題を解決することはできません。UITableViewAutomaticDimensionがカスタムテーブルセルで動作しない

私はこのテーブルセルの作成コードを持っている:

class UINoteTabelViewCell: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     self.selectionStyle = .none 
    } 

    func fill(_ note: Note) { 
     let view = UINote(withNote: note, atPoint: .zero) 
     self.contentView.addSubview(view) 
    } 
} 

これは、私は、テーブルビューのセルを作成する方法である:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "NotePreview", for: indexPath as IndexPath) as! UINoteTabelViewCell 

     cell.fill(notes[indexPath.row] as! Note) 
     return cell 
    } 

また、テーブルビューestimatedRowHeightrowHeightUITableViewAutomaticDimension

にセットされていますしかし、テーブルビューはまだ44.0行の高さで描画されます。

どのように修正するかわかりません。私はすべてのセルが動的高さ

+2

'estimatedRowHeight'は' UITableViewAutomaticDimension'であってはなりません。それを '100'に、' rowHeight'をUITableViewAutomaticDimensionに設定してみてください。 – TawaNicolas

+2

UITableViewAutomaticDimensionは制約を使用して動作します。セルがその高さを計算できる適切な制約を追加する必要があります – Abhishek

+0

lable number of line zero – karthikeyan

答えて

0

自動高さは、計算されたセルの高さで計算されます。contentViewしたがって、実際の高さを計算するために使用される制約を追加する必要があります。

これを試してみてください:

class UINoteTabelViewCell: UITableViewCell { 
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     self.selectionStyle = .none 
    } 

    func fill(_ note: Note) { 
     let view = UINote(withNote: note, atPoint: .zero) 
     self.contentView.addSubview(view) 

     // this will make the difference to calculate it based on view's size: 
     view.translatesAutoresizingMaskIntoConstraints = false 
     view.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true 
     view.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true 
     view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true 
     view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true 
    } 
} 

また、estimatedRowHeightは約すべてのセルの大きさを推定し、特定の値に設定する必要があります。しかし、それは問題ではないでしょう。

+0

最後にそれは仕事です!どうもありがとうございました! –

0

を持っているので、私はあなただけのセルcontentViewにNoteViewを追加している見ることができますestimatedRowHeightを固定設定することはできません

PS。また、先頭、末尾、上、下のようなノートビューに自動レイアウト制約を適用する必要があります。 ノートビューで正しい自動レイアウト制約を適用すると、それがうまくいきます。

0

あなたはestimatedRowHeightにいくつかデフォルト値を与える必要があり、その後の行が60のデフォルトの高さを持っていますが、内容はその時点で60以上の高さを必要とするたびUITableViewAutomaticDimensionが動作する60 を言います。

関連する問題