2017-04-13 65 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