2017-04-13 32 views
0

我试图在点击显示更多按钮标签后需要设置label.numberOfLines = 0后,在tableViewCell中实现动态标签高度。动态标签和单元格高度显示更多按钮动作

然后细胞高度和标签高度应该动态增加。

这里是我下面的代码(当tableView重新加载相同标签的高度以用于重复使用电池)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

let cell: UITableViewCell = 
self.tableView.dequeueReusableCell(withIdentifier: 
"BTSTableViewCellIdentifier")! 
    cell.configureWithPost(posts[indexPath.row]) 
    cell.delegate = self 
    return cell 
    } 

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

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


func configureWithPost(_ postViewModel: BTSTableCellViewModel) { 
    self.postViewModel = postViewModel 
    usernameLabel.text = postViewModel.username 
    detailtextLabel.numberOfLines = 2 
    detailtextLabel.text = postViewModel.textDetail 
} 

@IBAction func showmorePressed(_ sender: Any) { 
    detailtextLabel.numberOfLines = 0; 
     self.tableView.reload() 

} 
+1

返回'UITableViewAutomaticDimension'的高度索引路径行按下时ViewMore否则返回一些静态高度 – Dhiru

+0

@Bharath问题修复? –

+0

我已经实现了行的高度。但是,我仍然遇到这个问题。 @Ganesh – Bharath

回答

0
CGSize boundingBox = [label.text boundingRectWithSize:constraint 
               options:NSStringDrawingUsesLineFragmentOrigin 
              attributes:@{NSFontAttributeName:label.font} 
               context:context].size; 

size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height)); 
1

您还需要在heightForRowAtIndexPath管理高度:

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

而且in showMore Button Action,

@IBAction func showmorePressed(_ sender: Any) { 
    detailtextLabel.numberOfLines = 0; 
     self.tableView.reloadData() 
} 
+0

我仍然遇到同样的问题。你能帮我解决这个问题吗? – Bharath