Swift iOS自动为UITableViewCell调整高度UILabel()

问题描述:

如何根据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

未使用自动布局 –

首先你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