2017-05-14 91 views
0

因此,我试图创建一个适合的单元格的多行文本detailedTitle然而,当我设置cell.numberOfLines = 0我得到多行,但没有正确的单元格间距。我也打过电话cellAutoDimentions但是当我在我的程序swift 3:UITable单元格多行问题

enter image description here

运行的代码,你可以看到文本,如果得到推了它并没有区别

cell.detailTextLabel?.numberOfLines = 0 

我得到以下结果在这种情况下,我说的是顶部和底部不是右侧,这个问题我知道如何通过改变CGRect来解决。另外,代码不允许超过2行

UPDATE:

以下是我的代码的整个部分

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return itorStorage.count 
} 

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

    let cell = self.messageField.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MessageCustomCell 
    cell.backgroundColor = LightBlue 

    cell.textLabel?.text = itorStorage[indexPath.row].name 
    cell.textLabel?.textColor = .white 

    cell.detailTextLabel?.text = itorStorage[indexPath.row].text 
    cell.detailTextLabel?.textColor = .white 





    cell.pic.image = itorStorage[indexPath.row].image 

    cell.timeStamp.text = itorStorage[indexPath.row].time 

    cell.detailTextLabel?.numberOfLines = 0 
    cell.detailTextLabel?.lineBreakMode = .byWordWrapping 

    return cell 
} 



override init() { 

    super.init() 

    messageField.delegate = self 
    messageField.dataSource = self 
    messageField.tableFooterView = UIView(frame: CGRect.zero) 
    messageField.rowHeight = UITableViewAutomaticDimension 
    messageField.allowsSelection = false 

    self.messageField.register(MessageCustomCell.self, forCellReuseIdentifier: "Cell") 

    ratingView.didFinishTouchingCosmos = didTouchCosmos 


} 

回答

0

我认为你需要实现estimatedHeightForRowAt以及设置rowHeightUITableViewAutomaticDimension。事情是这样的:

override func viewDidLoad() { 
    super.viewDidLoad() 

    tableView.rowHeight = UITableViewAutomaticDimension 
} 

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

我想这个解决办法,但我得到相同的结果 – Sina

+0

您可以加入更多的代码(尤其是其他的tableview委托和数据源功能)的?你是否实现了heightForRow?如果是这样,请将其删除 – mlidal

+0

我在上面添加了额外的代码! – Sina