首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableView :在空部分中添加节页脚视图时崩溃

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

Stack Overflow用户
提问于 2009-12-12 15:12:42
回答 5查看 9.7K关注 0票数 11

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

但是,我没有找到任何解决这个问题的方法:我有一个有两个部分的UITableView,并且在应用程序第一次启动时没有行。用户可以在以后按自己的意愿填写部分(内容在这里不相关)。当填充行时,UITableView看起来很好,但当没有行时看起来很难看(两个标题部分粘在一起,中间没有空白)。这就是为什么在没有行的情况下,我想在中间添加一个很好的"No row“视图。

我使用了viewForFooterInSection:

代码语言:javascript
运行
复制
- (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节中没有行时,页脚视图才会出现。但是,当我进入编辑模式并删除第0节中的最后一行时,我的应用程序会崩溃:

在initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:终止应用程序中断言失败的原因是:“单元格动画停止分数必须大于开始部分”,这是由于-UIViewAnimation异常“NSInternalInconsistencyException”。

如果第0节中有几行,则不会发生这种情况。只有在只剩下一行时才会发生这种情况。

下面是编辑模式的代码:

代码语言:javascript
运行
复制
- (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];

    }
}

有人知道为什么会这样吗?谢谢

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-12-12 16:52:57

看起来就像苹果内部的UITableView代码假设,当你删除一行时,你的视图的第一部分会变短。通过使节页眉页脚突然变得更高以补偿最后一行,您似乎混淆了它。

两种想法供您尝试:

1.尝试使可选部分页脚比即将消失的表单元小一两个像素,这样动画代码就可以完成一两个像素的动画

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

这是一个例子,你只要接受苹果已经为你写了一半的程序,你必须遵从你的合著者做出的一些选择,不管你喜欢与否。

票数 9
EN

Stack Overflow用户

发布于 2012-09-06 10:08:51

  • 做空段
  • 使用本节中的表头,而不是第一节中的页脚。

  • (UIView *) tableView :(UITableView *)tableView viewForHeaderInSection:(NSInteger)节{ if (节!= OTHER_SECTION_INDEX) {返回0;}//您的代码在这里}- (CGFloat) tableView:(UITableView *) tableView heightForHeaderInSection:(NSInteger)节{ if (节!= OTHER_SECTION_INDEX) {返回0;}/您的代码}
票数 5
EN

Stack Overflow用户

发布于 2012-12-12 08:16:17

不应该同时调用deleteRowsAtIndexPaths:withAnimation:reloadData在tableView上。

你所看到的问题是我在对deleteRowsAtIndexPaths:withAnimation:的调用中看到的问题,因此对你来说,它永远不会到达reloadData

要想创建新的单元格、管理它们、处理必须处理不同单元格的所有位置,一个更简单的解决方案是在处理从0行到1行或1到0行的部分时使用reloadSections:withAnimation:

即替换以下代码行:

代码语言:javascript
运行
复制
        if(indexPath.section == 0)
        { [firstSectionArray removeObjectAtIndex:indexPath.row]; }
        else 
        { [secondSectionArray removeObjectAtIndex:indexPath.row]; }

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationBottom];

使用以下内容

代码语言:javascript
运行
复制
        BOOL useReloadSection;
        if (indexPath.section == 0)
        {
            [firstSectionArray removeObjectAtIndex:indexPath.row];
            useReloadSection = 0 == firstSectionArray.count;
        }
        else 
        {
            [secondSectionArray removeObjectAtIndex:indexPath.row];
            useReloadSection = 0 == secondSectionArray.count;
        }

        if (useReloadSection)
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
                     withRowAnimation:UITableViewRowAnimationBottom];
        else
            [tableView deleteRowsAtIndexPaths:@[indexPath]
                             withRowAnimation:UITableViewRowAnimationBottom];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1893712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档