动态标签和单元格高度,显示更多按钮操作

时间:2017-04-13 05:22:02

标签: ios objective-c swift3 reactive-cocoa rx-swift

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

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

以下是我的代码(当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()

}

2 个答案:

答案 0 :(得分:1)

您还需要管理heightForRowAtIndexPath:

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

同样在showMore Button Action中,

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

答案 1 :(得分:0)

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

size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height));
相关问题