这是我第一次在这里提出问题,但我不得不说,在过去的几个月里,这个网站对我有很大的帮助(iphone-dev),谢谢你。
但是,我没有找到任何解决这个问题的方法:我有一个有两个部分的UITableView,并且在应用程序第一次启动时没有行。用户可以在以后按自己的意愿填写部分(内容在这里不相关)。当填充行时,UITableView看起来很好,但当没有行时看起来很难看(两个标题部分粘在一起,中间没有空白)。这就是为什么在没有行的情况下,我想在中间添加一个很好的"No row“视图。
我使用了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节中的最后一行时,我的应用程序会崩溃:
在initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:curve:animateFromCurrentPosition:shouldDeleteAfterAnimation:editing:终止应用程序中断言失败的原因是:“单元格动画停止分数必须大于开始部分”,这是由于-UIViewAnimation异常“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];
    }
}有人知道为什么会这样吗?谢谢
发布于 2012-12-12 08:16:17
不应该同时调用deleteRowsAtIndexPaths:withAnimation:和reloadData在tableView上。
你所看到的问题是我在对deleteRowsAtIndexPaths:withAnimation:的调用中看到的问题,因此对你来说,它永远不会到达reloadData。
要想创建新的单元格、管理它们、处理必须处理不同单元格的所有位置,一个更简单的解决方案是在处理从0行到1行或1到0行的部分时使用reloadSections:withAnimation:。
即替换以下代码行:
        if(indexPath.section == 0)
        { [firstSectionArray removeObjectAtIndex:indexPath.row]; }
        else 
        { [secondSectionArray removeObjectAtIndex:indexPath.row]; }
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                         withRowAnimation:UITableViewRowAnimationBottom];使用以下内容
        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];https://stackoverflow.com/questions/1893712
复制相似问题