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

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


当前回答

我唯一能找到的解决办法就是这个

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...; // Instantiate with a "common" method you'll use again in cellForRowAtIndexPath:
    return cell.frame.size.height;
}

这是可行的,并且允许没有一个可怕的切换/如果复制StoryBoard中已经存在的逻辑。不确定性能,但我猜当到达cellForRow:细胞已经初始化,它是一样快。当然,这里可能会有附带损害,但看起来对我来说还行。

我还在这里发布了这个:https://devforums.apple.com/message/772464

编辑:Ortwin Gentz提醒我,heightForRowAtIndexPath:将被调用的所有单元格的TableView,而不仅仅是可见的。听起来很合理,因为iOS需要知道总的高度才能显示正确的滚动条。这意味着它可能在小的TableView(比如20个Cell)上很好,但在1000个Cell TableView上就忘了它了。

同样,前面关于XML的技巧:对我来说,与第一个注释相同。正确的值已经在那里了。

其他回答

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

这段代码:

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

感谢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...>

如果你用的是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
    }

}

如果你使用UITableViewController,实现这个方法:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

在行函数中,您可以选择高度。例如,

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

在本例中,第一行高度为100像素,其他行高度为60像素。

我希望这篇文章能帮到你。

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

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

实际上有两个地方需要更改行高,第一个是单元格(您已经更改了它) 现在选择表格视图并检查大小检查器