iphone - UITableView : crash when adding a section footer view in empty section

标签 iphone uitableview

这是我第一次在这里问问题,但我不得不说这个网站在过去几个月里给了我巨大的帮助(iphone-dev-wise),我为此表示感谢。

但是,我没有找到解决这个问题的方法: 我有一个包含 2 个部分的 UITableView,首次启动应用程序时没有行。用户可以稍后根据自己的意愿填写这些部分(内容与此处无关)。 UITableView 在充满行时看起来不错,但在没有行时看起来很丑(两个标题部分粘在一起,中间没有空格)。这就是为什么我想在没有行时在其间添加一个漂亮的“无行” View 。

我使用了viewForFooterInSection:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

if(section == 0)
{
    if([firstSectionArray count] == 0)
        return 44;
    else 
        return 0;
}

return 0;

}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if(section == 0)
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 10, 50, 44)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
        label.textAlignment = UITextAlignmentCenter;
        label.lineBreakMode = UILineBreakModeWordWrap; 
        label.numberOfLines = 0;
        label.text = @"No row";
        return [label autorelease];
    }

    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0)
    {
        return [firstSectionArray count];
    }
    return [secondSectionArray count];
}

这非常有效:仅当第 0 节中没有行时才会显示页脚 View 。 但是当我进入编辑模式并删除第 0 部分的最后一行时,我的应用程序崩溃了:

Assertion failure in -[UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:] Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Cell animation stop fraction must be greater than start fraction'

当第 0 节中有几行时,不会发生这种情况。仅当只剩下一行时,才会发生这种情况。

这是编辑模式的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    // If row is deleted, remove it from the list.
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Find the book at the deleted row, and remove from application delegate's array.

        if(indexPath.section == 0)
        { [firstSectionArray removeObjectAtIndex:indexPath.row]; }
        else 
        { [secondSectionArray removeObjectAtIndex:indexPath.row]; }

        // Animate the deletion from the table.
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationBottom];


        [tableView reloadData];

    }
}

有人知道为什么会发生这种情况吗? 谢谢

最佳答案

看起来发生的情况是,Apple 的 UITableView 内部代码假设当您删除一行时, View 的第一部分会变短。通过使节页眉页脚突然变高以补偿最后一行,您似乎会混淆它。

两个想法供您尝试:

1.尝试使可选部分页脚比即将消失的表格单元小一两个像素,以便动画代码可以执行一两个像素的动画

2. 当没有“真实”数据行时,不要删除表格中的唯一行,而是让表格仍然有 numberOfRowsInSection 返回 1 并制作一个假的“无数据”单元格,而不是在“无数据”情况下使用表页脚。

在这种情况下,你必须接受 Apple 已经为你编写了一半的程序,并且你必须遵守你的合著者所做的一些选择,无论你喜欢与否。

关于iphone - UITableView : crash when adding a section footer view in empty section,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1893712/

相关文章:

ios - 为什么每次加载新的 xib 文件时?为什么它不替换以前的文件?

iPhone - 后台轮询事件

iOS UIButton 或单行 UITableView 用于下拉菜单之类的东西?

ios - 如何使用 Rx 和嵌套单元格 View 突出显示 tableView 单元格?

ios - AVAudioPlayer 播放完毕时切换桌面 View 单元格图像

iPhone 应用程序 - 发布应用程序真的需要设置图标吗?

iphone - 创建自定义的 UITableView

ios - 最大支持的__attribute __((aligned(...))

ios - UITableViewCellaccessoryType什么时候统一的?

objective-c - 如何设置章节标题以使用 UITableView 样式分组?