2016-10-07 108 views
0

我有一個可擴展的tableView,其中當我展開一個部分時,比有三個單元格。在Firth Cell上,只有名字,在第二個單元格中。它有一個很大的內容。現在我想根據內容自動調整此標籤的高度和寬度。如何調整自定義tableView中的標籤高度和寬度單元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tblView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

     let dataArrayTblView = dataArrayForTableView 
     let titleName = dataArrayTblView.valueForKey("name") 
     let dueDate = dataArrayTblView.valueForKey("deadlinedate") 
     let description = dataArrayTblView.valueForKey("description") 

     cell.titleLabel.text = titleName[indexPath.row] as AnyObject! as! String! 
     cell.dueDateLabel.text = dueDate[indexPath.row] as? String 
     cell.descriptionLabel.text = description[indexPath.row] as? String 

     cell.descriptionLabel.sizeToFit() 


     cell.textLabel?.backgroundColor = UIColor.clearColor() 
     cell.selectionStyle = .None 
     return cell 
    } 

,但沒有得到完整的內容

enter image description here

+0

我沒有得到我的完整描述。 – iDeveloper

+0

http://stackoverflow.com/questions/39888512/tableview-cell-how-do-we-resize-cell-in-swift-along-with-image-and-label/39888662#39888662 –

+0

我沒有使用AutoLayout 。 – iDeveloper

回答

0

嘗試設置此。它會自動調整行的高度。如果它不工作,那麼你的故事板內的約束條件有問題。

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { 
    return 40 
} 

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension 
} 
+0

- 細胞 – iDeveloper

+0

它只顯示標題名稱後,該cell.descriptionLabel.adjustsFontSizeToFitWidth =真 – iDeveloper

+0

多少行,你呢?從你的回答中,我猜你對錶格視圖做的全部錯誤。 – jo3birdtalk

0

使用heightForRowAtIndexPath方法調整行的高度。用boundingRectWithSize這個方法計算字符串的大小 。例如:

試試這個:

if let YOUR_STRING:NSString = str as NSString? { 
let sizeOfString = ns_str.boundingRectWithSize(
           CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity),options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: lbl.font], context: nil).size 
} 
+0

獲得的內容要返回什麼值? – iDeveloper

+0

此方法將返回CGRect,並且您只返回sizeOfString.height,因爲最後我得到的是具有**的CGSize值。尺寸** –

0

你應該使用UITableViewAutomaticDimension爲行高度類似,

// this should be set in viewDidload 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 140 

爲您必須使用autolayout,並應設置適當的constraints在電池標籤。

約束應該是直鏈,我的意思是你的標籤的頂部和底部的約束必須設置你的標籤應根據內容調整大小讓你的細胞會相應調整!

您可以參考Self-sizing Table View Cells by Raywenderlich

+0

使用後,其僅顯示**標題**,DueDate和說明未顯示 – iDeveloper

相關問題