1

当我的表格视图为空时,我输入消息“无结果”。问题是,当我有 1 行并且我想删除此行时,行数不会改变,因为我有 if 条件显示“无结果”使用行,我可以创建 bool 属性来检查是否已删除,然后如果用户删除不会向他显示“无结果”消息的这一行,但也许有人有更好的方法?

我的方法:

节数:

if([[[self fetchResultsController] fetchedObjects] count] == 0)
        return 1;

部分中的行:

if([[[self fetchResultsController] fetchedObjects] count] == 0)
        return 1;

细胞:

if([[[self fetchResultsController] fetchedObjects] count] == 0){

        [[cell textLabel] setText:@"no results"];

    }else{ //create regular or from search cell.
4

2 回答 2

0

我找到的最佳解决方案是制作标题而不是单元格,以避免删除和更新(核心数据)出现问题。但是如果没有行或部分,我知道的标题就不会出现。所以我用这两种方法解决它:

-(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 才为空,标题似乎不是我想要的。

如果有人有更优雅的解决方案,请告诉我,我保证将您的答案作为正确答案。

于 2013-01-06T22:09:56.787 回答
0

为numberOfSectionsInTableView编写以下内容

if([[[self fetchResultsController] fetchedObjects] count] == 0)
        return 1;
else
        return [[[self fetchResultsController] fetchedObjects] count];

当您删除值时,从数组中删除对象并重新加载表。如果您遇到任何问题,请告诉我。

于 2013-01-07T10:40:30.787 回答