开发者

Obj-C, how do I show `no rows found` in a UITableView and allow all rows to be deleted, getting error?

I'm getting the following error...

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).

I've just added some code to show a row saying theres no data...

- (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

I've stepped through before the error is produced and it occurs after numberOfRowsInSection is completed. So its finding that theres a section it wasn't expecting.

How do I work around this ?

EDIT: Heres what happens just before the error

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


Do what the exception says. When you call -deleteRowsAtIndexPaths:withRowAnimation: to delete all rows in your table, insert one with -insertRowsAtIndexPaths:withRowAnimation: so that the number of rows is correct.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜