UITableView:在空白部分添加部分页脚视图时崩溃 [英] UITableView : crash when adding a section footer view in empty section

查看:105
本文介绍了UITableView:在空白部分添加部分页脚视图时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is the first time I ask a question here, but I have to say this site has been a tremendous help for me over the last couple months (iphone-dev-wise), and I thank you for that.

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

However, I didn't find any solution for this problem I'm having: I have a UITableView with 2 sections, and no rows when the app is launched for the first time. The user can fill the sections later on as he wishes (the content is not relevant here). The UITableView looks good when filled with rows, but looks pretty ugly when there is none (the 2 header sections are stuck together with no white space in between). That is why I'd like to add a nice "No row" view in between when there is no row.

我使用了viewForFooterInSection:

I used 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.
但是当我进入编辑模式并删除第0部分的最后一行时,我的应用程序崩溃了:

This works great : the footer view appears only when there is no row in section 0. But my app crashes when I enter edit mode and delete the last row in section 0:


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

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节中有多行时,不会发生这种情况。只有当只剩下一行时才会发生。

This does not happen when there are several rows in section 0. It only happens when there is only one row left.

以下是编辑模式的代码:

Here's the code for edit mode:

- (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];

    }
}

有没有人知道为什么会这样正在发生?
谢谢

Does anyone have any idea why this is happening? Thanks

推荐答案

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

It looks like what it happening is that internally Apple's UITableView code is assuming that when you delete a row, the first section of your view will get shorter. By making the section header footer suddenly get taller to compensate for the last row, you appear to be confusing it.

两个想法让你尝试:

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

1. try making the optional section footer a pixel or two smaller than the table cell that's going away, so that the animation code gets to do a pixel or two of animation

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

2. instead of deleting the only row of your table, when there are no "real" data rows let the table still have numberOfRowsInSection return 1 and make a fake "no data" cell rather than using a table footer for the "no data" case.

这是您必须接受Apple为您编写了一半程序的情况之一,您必须遵守您的共同作者所做出的一些选择,无论您是喜欢与否。

This is one of those cases where you just have to accept that Apple has written half of your program for you and you have to conform to some choices your co-author has made, whether you like them or not.

这篇关于UITableView:在空白部分添加部分页脚视图时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆