设置一个静态UITableViewCell的高度

时间:2015-07-03 14:03:01

标签: swift uitableview

我试图在Swift中设置静态UITableViewCell IBOutlet的高度。这可能吗?我已经找到了如何更改tableview中所有单元格的答案,但我只想更改这个单元格并保留其余部分,因为它们在IB中的大小。

我试过这个:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    if indexPath.section == 1 && indexPath.row == 1 {
        return CGFloat(screenSize.width)
    }
    else{   
        return UITableViewAutomaticDimension
    }
}

1 个答案:

答案 0 :(得分:0)

要在静态TableViewCell上动态设置高度,您必须确实在heightForRowAtIndexPath中设置它。以下是更改第一个部分中第二个行的一种实现:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {

    if indexPath.section == 0 && indexPath.row == 1 {
        // Here return a number for the height you desire
        return CGFloat(100)
    }
    else {
        return UITableViewAutomaticDimension
    }
}

以下是一些可能与您的情况相关的指示。如果他们在您的代码中故意这样做,请忽略它们。

单元格的高度设置为屏幕的宽度,可能值得检查是否有意。另请注意,部分和行都从索引0开始,而不是从索引1开始。