【发布时间】:2010-12-26 00:12:19
【问题描述】:
这是我第一次在这里提问,但我不得不说,在过去的几个月里,这个网站给了我很大的帮助(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];
}
这很好用:仅当第 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];
}
}
有人知道为什么会这样吗? 谢谢
【问题讨论】:
标签: iphone uitableview