故事板中的自定义单元格行高度设置没有响应

zir*_*isp 211 storyboard uitableview ios ios5 xcode4.2

我正在尝试调整表格视图中其中一个单元格的单元格高度.我正在从相关单元格的"尺寸检查器"中的"行高"设置中调整大小.当我在iPhone上运行应用程序时,单元格的默认大小设置为表格视图中的"行大小".

如果我更改表视图的"行大小",则所有单元格的大小都会更改.我不想这样做,因为我只想要一个单元格的自定义大小.我已经看到很多帖子都有针对该问题的程序化解决方案,但我更愿意通过故事板来实现,如果可能的话.

pix*_*eak 293

动态单元格rowHeight上,UITableView上的set总是覆盖单个单元格的rowHeight.

但是在静态单元格上,rowHeight在单个单元格上设置可以覆盖UITableView.

不确定这是不是一个bug,Apple可能是故意这样做的?

  • 正确答案#3,特别是因为这个答案适用于Interface Builder/Storyboards.如果在IB中选择_cell_,则_Size Inspector_在顶部显示行高(带有"自定义"复选框),但如果选择整个_table view_,_Size Inspector_也会在顶部显示行高(没有"自定义") " 在这种情况下).正如pixelfreak所说,只有表格视图设置用于动态单元格.(不确定是否有意) (34认同)
  • 这个答案意味着解决方案只是简单地将`UITableView`内容从`Dynamic Prototypes`改为`Static Cells`,我做到了,我的整个项目爆炸了......几乎自杀了. (8认同)
  • 有没有办法检索这个号码?深入挖掘的唯一方法是潜入故事板并从那里拉出来吗? (4认同)

Beb*_*ber 84

如果您使用UITableViewController,请实现此方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
Run Code Online (Sandbox Code Playgroud)

在行的功能中,您可以选择高度.例如,

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
       return 100;
    } 
    else {
       return 60;
    }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,第一行高度为100像素,其他行为60像素.

我希望这个可以帮助你.

  • Beber,是否总是必须同时执行这两项操作(选中自定义并编辑storyboard中的行高度值&&在heightForRowAtIndexPath中指定它)? (2认同)

mem*_*ons 34

对于动态单元格,rowHeight设置UITableView始终覆盖单个单元格rowHeight.

这个行为是,IMO,一个bug.任何时候你必须在两个地方管理你的UI很容易出错.例如,如果您在故事板中更改单元格大小,则必须记住也要更改它们heightForRowAtIndexPath:.在Apple修复bug之前,当前最好的解决方法是覆盖heightForRowAtIndexPath:,但使用故事板中的实际原型单元来确定高度而不是使用幻数.这是一个例子:

- (CGFloat)tableView:(UITableView *)tableView 
           heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* In this example, there is a different cell for
       the top, middle and bottom rows of the tableView.
       Each type of cell has a different height.
       self.model contains the data for the tableview 
    */
    static NSString *CellIdentifier;
    if (indexPath.row == 0) 
        CellIdentifier = @"CellTop";
    else if (indexPath.row + 1 == [self.model count] )
        CellIdentifier = @"CellBottom";
    else
        CellIdentifier = @"CellMiddle";

    UITableViewCell *cell = 
              [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    return cell.bounds.size.height;
}
Run Code Online (Sandbox Code Playgroud)

这将确保在运行时自动获取原型单元格高度的任何更改,您只需在一个位置管理UI:故事板.

  • 我发布了一个包含缓存的完整代码的答案; 见http://stackoverflow.com/a/16881312/292166 (2认同)

Jos*_*phH 22

我已经构建了各种答案/注释提示的代码,以便这适用于使用原型单元的故事板.

这段代码:

  • 不需要将单元格高度设置在故事板中明显位置以外的任何位置
  • 出于性能原因缓存高度
  • 使用公共函数获取索引路径的单元标识符,以避免重复逻辑

感谢Answerbot,Brennan和lensovet.

- (NSString *)cellIdentifierForIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = nil;

    switch (indexPath.section)
    {
        case 0:
            cellIdentifier = @"ArtworkCell";
            break;
         <... and so on ...>
    }

    return cellIdentifier;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];
    static NSMutableDictionary *heightCache;
    if (!heightCache)
        heightCache = [[NSMutableDictionary alloc] init];
    NSNumber *cachedHeight = heightCache[cellIdentifier];
    if (cachedHeight)
        return cachedHeight.floatValue;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    CGFloat height = cell.bounds.size.height;
    heightCache[cellIdentifier] = @(height);
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = [self cellIdentifierForIndexPath:indexPath];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    <... configure cell as usual...>
Run Code Online (Sandbox Code Playgroud)


小智 19

实际上有两个地方需要更改行高,首先是单元格(您已经更改过),现在选择表格视图并检查尺寸检查器


Fog*_*gni 11

我认为这是一个错误.

尝试不是通过实用程序检查器调整高度,而是直接在故事板上拖动鼠标.

我用这种方法解决了这个问题.


Cha*_*lva 10

如果您使用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
    }

}
Run Code Online (Sandbox Code Playgroud)


Hir*_*hal 7

您可以通过以下行从故事板获取UITableviewCell(在UITableviewController - 静态单元格中)的高度.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CGFloat height = [super tableView:tableView heightForRowAtIndexPath:indexPath];

    return height;
}
Run Code Online (Sandbox Code Playgroud)


Sht*_*lic 6

在XML视图中打开storyboard并尝试编辑rowHeight所需元素的属性.

当我尝试为我的原型行设置自定义rowHeight时,它对我有用.它不是通过检查员工作,但通过XML工作.


Hen*_*rtz 6

您可以使用cells自定义原型height,然后调用cellForRowAtIndexPath:并返回它frame.height.:.

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