2016-10-19 1 views
0

セルごとに高さも変更されているため、UITableViewカスタムセルを作成しています。iIT10でSwift3でUITableViewカスタムセルオーバーラップ

これは私のコードを初期化するためのセルです。その後、私はSubviewとしてUITextViewを追加したいと思います。

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary 

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0) 
    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)" 

    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16) 

    return heightTitle+5; 
} 


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let dictAd = self.arrAllQuestions[indexPath.row] as! NSDictionary 

    let cell = tableView.dequeueReusableCell(withIdentifier: "quesionCell", for: indexPath) as! QuestionCell 

    let fontNew = UIFont(name: "AvenirNext-Regular", size: 19.0) 

    let strA = "\(indexPath.row+1). \(dictAd.object(forKey: "Title")!)" 

    cell.lblName.font = fontNew 
    cell.lblName.text = strA 
    cell.lblName.numberOfLines = 0 
    let heightTitle = self.heightForView(text: strA, font: fontNew!, width: tableView.frame.size.width-16) 

    var frame = cell.lblName.frame as CGRect 
    frame.size.height = heightTitle; //you need to adjust this value 
    cell.lblName.frame = frame; 


    let txtView = AnimatableTextView(frame: CGRect(x: 8, y: cell.lblName.frame.origin.y+cell.lblName.frame.size.height+5, width: tableView.frame.size.width-16, height: 25)) 
    txtView.borderWidth = 1.0 
    txtView.borderColor = UIColor.lightGray 
    txtView.cornerRadius = 4.0 
    cell.contentView.addSubview(txtView) 

    return cell 
} 

出力は次のとおりです。

enter image description here

+0

に役立ちます。私の推測では、デリゲートメソッドで各セルの正しい高さを設定していないということです。 –

+0

'heightForRowAt'を実装していますか? – Adeel

+0

ストーリーボードからラベルとのTextViewを追加し、ストーリーボード – Neha

答えて

3

あなたのheightForRowAtIndexPathでの高さの計算が適切ではないようです。問題を解決するには、UITableViewAutomaticDimensionとAutolayoutを使ってセルフサイジングセルを使用することを検討してください。 Hereは、使い始めるのに役立つ素晴らしいチュートリアルです。セル内でUITextViewまたはそのサブクラスを使用していて、コンテンツの高さを取得してスクロール可能なプロパティをfalseに設定する必要がある場合はもう1つの提案です。 viewDidLoad ホープの

+0

UITableViewAutomaticDimensionによって制約を適用すると、自動レイアウトを使用する場合、最良の答えです。 – negaipro

+0

私は自動レイアウトを使用していません... –

+0

次に、私が言ったように、heightForRowAtIndexPathであなたの高さの計算をチェックしてください。 –

1

宣言これは、正しい結果だとquesionCell` `のコードが何かについて、あなたの疑問を改善することをお勧めします

tableView.estimatedRowHeight = 44 
関連する問題