2017-02-25 178 views
1

我想设置动态基于详细文本标签设置的内容行的高度,通过使用部分A.下面的代码的iOS斯威夫特动态调整的tableview单元格的高度根据内容 -

我将几行文本插入到单元格的详细文本标签中,如下面的B部分所示:

我已经查看了其他类似的问题,但都没有帮助。

有人可以请教我如何根据详细文本标签的内容动态调整行高度。

A部分

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

也试过

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     return UITableViewAutomaticDimension 
    } 

B部分

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "daysIdentifier", for: indexPath) 

     cell.textLabel?.text = days[indexPath.row] 

     cell.detailTextLabel?.numberOfLines = 0 

     var joinedString   = self.availabilityTimeDict[dayName]?.joined(separator: " \n ") 
     cell.detailTextLabel?.text = joinedString 
     return cell 
    } 

enter image description here

+2

设置标签的顶部和底部限制正确,也设置tableView.estimateRowHeight = 50 –

回答

8

使用自定义单元格和标签。 设置UILabel的约束。 (顶部,左边,底部,右) 所述的UILabel的集线为0

添加以下代码中的ViewController的viewDidLoad方法:

tableView.estimatedRowHeight = 68.0 
tableView.rowHeight = UITableViewAutomaticDimension 

//代表&数据源

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
    return UITableViewAutomaticDimension; 
} 

夫特4:

//代表&数据源

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

给予顶部,底部,开头和结尾下面表视图

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
{ 
return 100 
} 
0
  • 首先建立一个自定义单元格的方法tableview.Use您拉布勒里面的内容,并添加一个标签并将其行数设置为零,并给单元格的内容视图(不给高度)的底部,顶部,引导,尾部约束也给单元的大小检查器自定义高度,然后在viewDidLoad你只需要做,

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 100.0

相关问题