我找到的最佳解决方案是制作标题而不是单元格,以避免删除和更新(核心数据)出现问题。但是如果没有行或部分,我知道的标题就不会出现。所以我用这两种方法解决它:
-(void) hideTitle{
self.tableView.tableHeaderView = nil;
}
-(void) showTitle{
self.contener = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 40)];
self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
self.label.text = NSLocalizedString(@"Header for the table", @"");
self.label.shadowOffset = CGSizeMake(0, 1);
self.label.font = [UIFont boldSystemFontOfSize:22];
self.label.backgroundColor = [UIColor redColor];
[self.contener addSubview:[self label]];
self.tableView.tableHeaderView = self.contener;
}
我把这个名为 show 的东西放在了num_of_sections..:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if([[[self fetchResultsController] fetchedObjects] count] == 0)
[self showTitle]; .....}
此方法在删除行时效果很好。创建新行时使用的第二种名为 hide 的方法(我将该方法放在“保存方法”中)此方法删除标题。
编辑:
另一种方式:
当我在行数之前尝试...
if([[bla bla] count] == 0 && is_deleted == 0)
return 1;
也改变细胞:
if([[bla bla] count] == 0){
[[cell textLabel]setText:@"no results"];
[[cell detailTextLabel] setText:nil];
cell.accessoryType = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
}else{
cell.userInteractionEnabled = YES;
cell.accessoryType = UITableViewCellSelectionStyleBlue;
... }
问题 id 是一个删除行,所以我使用 bool 来避免在didLoad method bool = 0;
我delete I insert 1 to the bool
不忘记取消此单元格的删除选项时被迷住!
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete && [[bla bla] count] != 0)
{ ... }
这个解决方案的问题是它并不完美,当我删除最后一行时,只有当我再次加载表时,我的 tableView 才为空,标题似乎不是我想要的。
如果有人有更优雅的解决方案,请告诉我,我保证将您的答案作为正确答案。