这是我第一次在这里问一个问题,但我不得不说这个网站在过去几个月对我来说是一个巨大的帮助(iphone-dev-wise),我感谢你。
但是,我找不到任何有关此问题的解决方案: 我有一个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: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];
}
}
有谁知道为什么会这样? 谢谢