簡體   English   中英

帶有Swift3的ios10中的UITableView自定義單元重疊

[英]UITableView Custom Cell Overlapping in ios10 with Swift3

我正在制作UITableView自定義單元格,因為每個單元格的高度也發生了變化。

這是我用於初始化單元格的代碼,之后我想將UITextView添加為Subview。

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
}

您可以在下面看到輸出。

在此處輸入圖片說明

看來您的heightForRowAtIndexPath中的高度計算不正確。 考慮使用通過UITableViewAutomaticDimension和Autolayout調整大小的單元格來解決您的問題。 是一個很棒的教程,可以幫助您入門。 如果在單元格中使用UITextView或相同類的子類,並且希望它占用內容的高度,則將其可滾動屬性設置為false,這是另一個建議。

在viewDidLoad中聲明此內容,希望對您有所幫助

tableView.estimatedRowHeight = 44

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM