iOS8 Swift - 动态调整tableview单元格大小的问题

时间:2015-01-03 08:12:41

标签: swift dynamic ios8 resize tableview

Here's what my application looks like currently.

你会看到,当字幕文本标签只有一行正确调整大小时,但是当有多行时它会全部混乱。我认为这可能与约束有关?现在我正在使用自动布局约束。您可以在屏幕截图中看到它们。这是创建我的单元格的代码。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> DealCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Deal Cell", forIndexPath: indexPath) as DealCell

    let currentBar = bars[indexPath.row] as BarAnnotation
    cell.barName.text = currentBar.name
    cell.deal.text = currentBar.deal
    cell.distanceToBar.text = String(format: "%.3f mi", currentBar.distance)

    // Set the height of the table view cells
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0;

    return cell
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在UITableViewDelegate中使用tableView:heightForRowAtIndexPath:函数。 使用此功能,您可以单独设置每行的高度。

有关详细信息:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:heightForRowAtIndexPath

或者您可以使用本文中的自动布局设置。 http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html