2009-12-12 90 views
11

这是我第一次在这里提出问题,但我不得不说这个网站在过去的几个月里对我有很大的帮助(iphone-dev-明智的),我感谢你。UITableView:在空白部分添加节页脚视图时崩溃

但是,我没有找到任何解决方案,我有这个问题: 我有一个UITableView与2节,并没有行时,应用程序第一次启动。用户可以按照自己的意愿稍后填写章节(内容与此无关)。当用行填充时,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]; 
} 

这个伟大的工程:只有当在第0 无行,但出现的页脚视图我的应用程序崩溃,当我进入编辑模式,并删除部分最后一行0:

断言故障 - [UIViewAnimation initWithView:indexPath:endRect:endAlpha:startFraction:endFraction:曲线:animateFromCurrentPosition:shouldDeleteAfterAnimation:编辑:] 终止应用程序由于未捕获的异常“NSInterna lInconsistencyException”,理由是:‘细胞停止动画分数必须大于开始部分’

此当有节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]; 

    } 
} 

没有人有任何想法,为什么发生这种情况? 谢谢

回答

9

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

两种思路供您试用:

尝试使可选部分页脚比要出门的表格单元格小的像素或两个,让动画代码获取做一个像素两个动画,而不是删除您表的唯一行

2.,当没有“真正”的数据行让表仍然有numberOfRowsInSection返回1,使假的“无数据”单元,而不是使用表格页脚来处理“无数据”的情况。

这是其中的一种情况,您只需接受Apple为您编写的一半程序,并且必须符合您的合着者所做的一些选择,无论您是否喜欢它们。

+1

你是我的英雄!将“无数据”单元缩小2个像素使其全部工作!非常感谢! (和FYI,我已经想到了第二个解决方案,但无法使其工作) 但我仍然有一个小问题:tableview的背景是白色,现在当第0部分没有行时,我更喜欢它。它充满了空单元格(请参阅http://yfrog.com/izuitableviewproblemp)。任何提示? 再次谢谢大卫,你让我的一天! – nmondollot 2009-12-12 17:33:46

+0

那里没有很棒的想法,也许值得一个单独的问题。 – 2009-12-12 18:00:04

+0

好的,这就是我所做的。谢谢 – nmondollot 2009-12-12 18:22:52

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 
} 
+0

什么是“OTHER_SECTION_INDEX”应该是? – LightningStryk 2013-04-08 16:41:56

+0

我的示例使用header_view上的footer_view进行更改 - 因此您需要添加下面的空白部分以使用header_view。 在这种情况下,OTHER_SECTION_INDEX == 0和header_view将在第1部分 – comonitos 2013-04-10 10:05:40

+0

不适用于我。 – 2014-06-24 11:37:51

1

它不应该有必要呼吁两国deleteRowsAtIndexPaths:withAnimation:reloadData的tableView。

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

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

即替换下面的代码行:用以下

 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]; 
+0

谢谢!很好的答案! – FunkyKat 2014-06-25 12:07:42

1

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

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

我遇到同样的崩溃,看起来就像是一个bug in the OS

我的具体使用案例涉及具有可折叠/可展开部分的UITableView。我需要部分分隔线(这是我使用的部分页脚),但不是行分隔线(不能简单地使用单元格分隔符)。

我无法通过调整节页脚的高度来解决问题。但切换到使用部分标题做了窍门,错误/崩溃不再发生。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    CGRect frame = (CGRect){0, 0, self.view.bounds.size.width, 1}; 
    UIView *view = [[UIView alloc] initWithFrame:frame]; 
    view.backgroundColor = [UIColor commentReplyBackground]; 

    return view; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 1.0f; 
} 
0

对于那些仍然需要部分页脚的人,您也可以尝试将TableView样式更改为UITableViewStyleGrouped。这似乎绕过了这个问题。为了获得相同的视觉效果,您只需将所有其他页脚和标题设置为CGFLOAT_MIN即可。

相关问题