UITableView:在空部分中添加部分页脚视图时崩溃

nmo*_*lot 11 iphone uitableview

这是我第一次在这里提出一个问题,但我不得不说这个网站在过去几个月对我来说是一个巨大的帮助(iphone-dev-wise),我感谢你.

但是,我找不到任何有关此问题的解决方案:我有一个带有2个部分的UITableView,并且第一次启动应用程序时没有行.用户可以根据自己的意愿稍后填写这些部分(这里的内容不相关).UITableView在填充行时看起来很好,但是当没有行时看起来很丑陋(2个标题部分粘在一起,中间没有空格).这就是为什么我想在没有行时添加一个漂亮的"无行"视图.

我使用了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];
}
Run Code Online (Sandbox Code Playgroud)

这很好用:仅当第0部分中没有行时才会显示页脚视图.但是当我进入编辑模式并删除第0部分中的最后一行时,我的应用程序崩溃:

断言失败 - [UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:]由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'单元格动画停止分数必须大于开始分数"

当第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];

    }
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么会这样?谢谢

Dav*_*des 9

看起来它发生的是内部Apple的UITableView代码假设当你删除一行时,你视图的第一部分会变短.通过使节标题页脚突然变得更高以补偿最后一行,您似乎混淆了它.

您尝试的两个想法:

1. 尝试使可选的部分页脚比正在消失的表格单元格小一个或两个像素,这样动画代码就可以做一两个动画像素

2.没有删除表中唯一的行,当没有"真正的"数据行时,让表仍然numberOfRowsInSection返回1并伪造"无数据"单元而不是使用表格页脚来表示"无数据"情况.

这是其中一种情况,您只需要接受Apple为您编写了一半的程序,并且您必须遵守您的共同作者所做出的一些选择,无论您是否喜欢.


com*_*tos 5

  • 制作空节
  • 在本节中使用表头而不是第一部分中的页脚
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section != OTHER_SECTION_INDEX) {
        return nil;
    }
    //your code here
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section != OTHER_SECTION_INDEX) {
        return 0;
    }
    //your code
}
Run Code Online (Sandbox Code Playgroud)