Obj-C,我如何在UITableView中显示`no rows found`并允许删除所有行,得到错误?

时间:2011-02-05 10:51:14

标签: objective-c uitableview

我收到以下错误...

Terminating app due to uncaught exception NSInternalInconsistencyException, 
reason: Invalid update: invalid number of rows in section 0.  The number of 
rows contained in an existing section after the update (1) must be equal to 
the number of rows contained in that section before the update (1), plus or 
minus the number of rows inserted or deleted from that section 
(0 inserted, 1 deleted).

我刚刚添加了一些代码来显示一行,说没有数据......

- (NSInteger)tableView:(UITableView *)tableView 
       numberOfRowsInSection:(NSInteger)section {

    NSInteger num = [self.transactionsArray count];

    if (num == 0) {
        num = 1;
        [dataTableView setEditing: FALSE animated: NO];
    }
    return num;
}

- (UITableViewCell *)tableView:(UITableView *)tableView 
       cellForRowAtIndexPath:(NSIndexPath *)indexPath {

     UITableViewCell *cell1;
    if(transactionsArray.count == 0) {
        self.navigationItem.leftBarButtonItem.enabled = FALSE;
        cell1 = [[[UITableViewCell alloc] initWithStyle:
               UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];

        cell1.textLabel.text = @"No transactons found";
        return cell1;
     }
         //Normal processing

我在错误产生之前已经介入,并且在numberOfRowsInSection完成后发生。所以它发现了一个它没有预料到的部分。

我该如何解决这个问题?

编辑: 下面是错误之前发生的事情

tableView:commitEditingStyle:forRowAtIndexPath:] [Line 630] commitEditingStyle start
tableView:commitEditingStyle:forRowAtIndexPath:] [Line 633] Delete now!
numberOfSectionsInTableView:] [Line 447] numberOfSectionsInTableView start
numberOfSectionsInTableView:] [Line 447] numberOfSectionsInTableView start
tableView:numberOfRowsInSection:] [Line 456] numberOfRowsInSection start

1 个答案:

答案 0 :(得分:0)

执行异常所说的内容。当您致电-deleteRowsAtIndexPaths:withRowAnimation:删除表格中的所有行时,请插入-insertRowsAtIndexPaths:withRowAnimation:一行,以便行数正确无误。