2016-10-07 112 views
0

我有一个可扩展的tableView,其中当我展开一个部分时,比有三个单元格。在Firth Cell上,只有名字,在第二个单元格中。它有一个很大的内容。现在我想根据内容自动调整此标签的高度和宽度。如何调整自定义tableView中的标签高度和宽度单元格

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = tblView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomTableViewCell 

     let dataArrayTblView = dataArrayForTableView 
     let titleName = dataArrayTblView.valueForKey("name") 
     let dueDate = dataArrayTblView.valueForKey("deadlinedate") 
     let description = dataArrayTblView.valueForKey("description") 

     cell.titleLabel.text = titleName[indexPath.row] as AnyObject! as! String! 
     cell.dueDateLabel.text = dueDate[indexPath.row] as? String 
     cell.descriptionLabel.text = description[indexPath.row] as? String 

     cell.descriptionLabel.sizeToFit() 


     cell.textLabel?.backgroundColor = UIColor.clearColor() 
     cell.selectionStyle = .None 
     return cell 
    } 

,但没有得到完整的内容

enter image description here

+0

我没有得到我的完整描述。 – iDeveloper

+0

http://stackoverflow.com/questions/39888512/tableview-cell-how-do-we-resize-cell-in-swift-along-with-image-and-label/39888662#39888662 –

+0

我没有使用AutoLayout 。 – iDeveloper

回答

0

尝试设置此。它会自动调整行的高度。如果它不工作,那么你的故事板内的约束条件有问题。

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

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

- 细胞 – iDeveloper

+0

它只显示标题名称后,该cell.descriptionLabel.adjustsFontSizeToFitWidth =真 – iDeveloper

+0

多少行,你呢?从你的回答中,我猜你对表格视图做的全部错误。 – jo3birdtalk

0

使用heightForRowAtIndexPath方法调整行的高度。用boundingRectWithSize这个方法计算字符串的大小 。例如:

试试这个:

if let YOUR_STRING:NSString = str as NSString? { 
let sizeOfString = ns_str.boundingRectWithSize(
           CGSizeMake(self.titleLabel.frame.size.width, CGFloat.infinity),options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: lbl.font], context: nil).size 
} 
+0

获得的内容要返回什么值? – iDeveloper

+0

此方法将返回CGRect,并且您只返回sizeOfString.height,因为最后我得到的是具有**的CGSize值。尺寸** –

0

你应该使用UITableViewAutomaticDimension为行高度类似,

// this should be set in viewDidload 
    tableView.rowHeight = UITableViewAutomaticDimension 
    tableView.estimatedRowHeight = 140 

为您必须使用autolayout,并应设置适当的constraints在电池标签。

约束应该是直链,我的意思是你的标签的顶部和底部的约束必须设置你的标签应根据内容调整大小让你的细胞会相应调整!

您可以参考Self-sizing Table View Cells by Raywenderlich

+0

使用后,其仅显示**标题**,DueDate和说明未显示 – iDeveloper

相关问题