2012-01-28 76 views
1

这是我的代码在我的UITableView删除单元格:UITableView-删除最后一个单元部分

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [tableView beginUpdates]; 

    if([tableView numberOfRowsInSection:[indexPath section]] > 1) { 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
      withRowAnimation:UITableViewRowAnimationFade]; 
    } else { 
     [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
      withRowAnimation:UITableViewRowAnimationFade]; 
    } 

    [tableView endUpdates]; 
} 

还有被删除单元格之前被更新了一些数据,但我宁愿相信这是正确更新,因为数据源数组的count每次删除一个单元时总是少一个。这是预期的行为。然而,出于某种原因,每当我删除最后一个单元格中的一部分,我得到'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) 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, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

这是非常令人沮丧,尤其是从什么,因为我已经能够在网上找到,我正确地做这个。也许我需要睡眠,这是一段时间。这里的任何想法?

解决方案:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
    if(tableIsEmpty) { 
     //**this** line was the culprit- returning zero here fixed the problem 
     return 0; 
    } else if(section == ([tableView numberOfSections] - 1)) { 
     //this is the last section- it only needs one row for the 'Delete all' button 
     return 1; 
    } else { 
     //figure out how many rows are needed for the section 
    } 
} 

回答

4

你的问题就出在你numberOfRowsInSection:方法的实现。您的删除似乎很好。当您删除行时,我认为您无法更新用于返回numberOfRowsInSection:方法中的行数的源。由于那里的不一致,你会得到错误。请分享该代码。

+0

是的,是的!你完全正确。我想我需要的只是有人告诉我,我的问题*不是我的删除代码。我弄明白了 - 这与我总是用“全部删除”单元显示一个额外部分有关,而当最后一部分被删除时,我没有让它消失。总之,你帮了很多忙,所以谢谢你! – 2012-01-28 06:26:42

+0

@Rickay很高兴为您提供帮助。 – MadhavanRP 2012-01-28 07:08:08

相关问题