2016-10-19 75 views
0

我正在製作UITableView自定義單元格,因爲每個單元格的高度也發生了變化。UITableView自定義單元格在ios10中與Swift3重疊

這是我的初始化單元的代碼,之後我想添加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

這是更好地提高你對什麼是正確的結果,什麼是對'quesionCell'代碼問題。我的猜測是你沒有爲委託方法中的每個單元格設置正確的高度。 –

+0

你正在執行'heightForRowAt'嗎? – Adeel

+0

從storyboard添加標籤和textview,並通過storyboard應用約束 – Neha

回答

3

看來,在你的heightForRowAtIndexPath的高度計算是不妥當的。考慮使用UITableViewAutomaticDimension和Autolayout來解決自己的問題。 Here是一個很好的教程,可以幫助你開始。如果您在單元格中使用UITextView或其子類並且希望它將內容的高度設置爲可滾動屬性爲false,那麼還有一個建議。

+0

如果您使用自動佈局,則UITableViewAutomaticDimension是最好的答案。 – negaipro

+0

我沒有使用自動佈局... –

+0

然後,正如我說的檢查你的身高計算在heightForRowAtIndexPath。 –

1

聲明此在viewDidLoad中 希望這將幫助你

tableView.estimatedRowHeight = 44