2016-04-26 101 views
0

如何根據textLabel的長度自動調整每個單元格的高度?理想情況下,我希望textLabel也能被包裝,所以textLabel可以使用它所需的多條線。下面的代碼是什麼我目前:Swift iOS自動爲UITableViewCell調整高度UILabel()

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let notification = self.fetchedTweetsArray[indexPath.row] 

     let cell = UITableViewCell() 
     cell.textLabel?.text = notification 
     return cell 
    } 
+0

你在使用autolayout嗎? – ivarun

+0

未使用自動佈局 –

回答

0

首先你cellForRowAtIndex應該是這樣的

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

cell.selectionStyle = UITableViewCellSelectionStyle.None 

cell.lblData.text = "CustomCell....." 
return cell 

} 

爲行的UILabel沒有應爲零。

enter image description here

現在按的UILabel的高度,你必須計算動力高度爲每個小區保持在可變的數組,以便以後你可以在heightForRowAtIndex使用。

確保自動調整大小像這樣的的UILabel

enter image description here

剛剛作出了一個示例代碼,你可以下載和觀察。

Download sample code