我想增加我的UITableviewCell高度取決於TableViewCell上的UILabel內容按鈕單擊。 我有搜索,但我找到約束的答案。swift:增加TableViewCell高度取決於UILabel而不使用約束
我想在不使用'Constarints'/ Autolayout的情況下動態增加UITabelViewCell高度。
我用下面的代碼獲取標籤身高:
func getLabelHeight(lbl : UILabel) -> CGFloat
{
let constraint = CGSizeMake(lbl.frame.size.width, CGFloat.max)
var size = CGSize()
let context = NSStringDrawingContext();
let boundingBox : CGSize
boundingBox = NSString(string: lbl.text!).boundingRectWithSize(constraint,
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: context).size
size = CGSizeMake(ceil(boundingBox.width), ceil(boundingBox.height))
return size.height;
}
,但我不能在UITableView中使用此代碼。爲了提高TableViewCell高度我已經寫了下面的代碼:
func btnReadMoreClick(sender: UIButton){
let buttonTag = sender.tag
indexOfReadMoreButton = sender.tag
isReadMoreButtonTouched = true
tableVDetail.beginUpdates()
tableVDetail.reloadRowsAtIndexPaths([NSIndexPath(forItem: indexOfReadMoreButton, inSection: 0)], withRowAnimation: UITableViewRowAnimation.Automatic)
tableVDetail.endUpdates()
}
,並設置單元格的高度在
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
但是使用這個我不能讓我的輸出。 :(
提前感謝!
如果你真的想這樣做,使這個函數來計算高度靜態和直接傳入文本進行綁定,所以你可以輕鬆地調用CellName.getLabelHeight ...在heightForRowAtIndex .... – mbutan
@mbutan你可以解釋一下我使用代碼。或者如果你有另一種方式,那麼也請告訴他們。 –