我试图调整我的表格视图中的一个单元格的高度。我正在调整单元格的“大小检查器”内的“行高”设置的大小。当我在我的iPhone上运行应用程序时,单元格的默认大小设置自表格视图中的“行大小”。

如果我改变了表格视图的“行大小”,那么所有单元格的大小都会改变。我不想这样做,因为我只想为一个单元格自定义大小。我已经看到了很多关于这个问题的程序化解决方案的帖子,但如果可能的话,我更喜欢通过故事板来实现。


当前回答

如果你用的是swift,就像这样用。不要使用故事板来选择行高。像这样通过编程设置表行高,

 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    if indexPath.row == 0 || indexPath.row == 1{
        let cell = self.tableView.dequeueReusableCellWithIdentifier("section1", forIndexPath: indexPath) as! Section1TableViewCell
        self.tableView.rowHeight = 150
        cell.label1.text = "hiiiiii"
        cell.label2.text = "Huiiilllllll"
        return cell

    } else {

        let cell = self.tableView.dequeueReusableCellWithIdentifier("section2", forIndexPath: indexPath) as! Section2TableViewCell
        self.tableView.rowHeight = 60
        cell.label3.text = "llll"
        return cell
    }

}

其他回答

在XML视图中打开故事板,并尝试编辑所需元素的rowHeight属性。

当我试图为我的原型行设置自定义rowHeight时,它为我工作。 它不能通过检查器工作,但可以通过XML工作。

作为评论添加,但作为可见性的答案发布:

如果最初将表视图设置为“静态单元格”,然后将其更改为“动态原型”,似乎也会出现问题。我有一个问题,甚至委托方法heightForRowAtIndexPath被忽略。我首先通过直接编辑故事板XML来解决这个问题,然后最终从头开始重新构建故事板场景,从一开始就使用动态原型。

我认为这是一个bug。

尝试调整高度不是通过实用工具检查器,而是直接通过鼠标拖动故事板。

我用这个方法解决了这个问题。

您可以使用具有自定义高度的原型单元格,然后调用cellForRowAtIndexPath:并返回其frame.height.:。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView
                    cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}

如果你想设置一个静态行高,你可以这样做:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}