2015-04-06 74 views
0

我正在删除UITableView中的所有项目。因此,我加载UITableView的数组的count = 0。删除数组中的最后一项后,重新加载表时,出现错误numberofRowInSectionUITableView reloadData与空数组

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

    return [arrProjects count]; 
} 


- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@""); 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     [tableProject beginUpdates]; 
    Project *project = [arrProjects objectAtIndex:indexPath.row]; 
    [[CommonModel shared]DeleteProjectDetails:project.ProjectId]; 
    [arrProjects removeObject:project]; 
    [self reloadTableProject:YES]; 
    [tableProject endUpdates]; 
    } 

} 


-(void) reloadTableProject:(BOOL)isReloadRequired 
{ 
    //[arrProjects removeAllObjects]; 
    arrProjects = [[CommonModel shared]GetAllProjects]; 

    if(isReloadRequired) 
     [tableProject reloadData]; 
} 

这是错误:

“无效更新:在部分0的行的无效数更新后的包含在现有部分 行数(1)必须是 等于到 update(2)之前包含在该段中的行数,加上或减去从 插入或删除的行数(0插入的,0删除的)和正数或负数的 行移入或在该部分中(0个移入,0个移出)。'

我每次都得到这个错误,不仅是当数组是空的。

+0

有你设置的numberofRowInSection为您的数据源阵列的数量? –

+0

你可以发布你删除该数组中所有对象的代码 – EmilDo

+0

@LeeJPollard:是的,我有。 – Nitish

回答

2

集:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [_datasourceArray count]; 
} 

要清空你的TableView,做到:

[_datasourceArray removeAllObjects]; 
[_tableView reloadData]; 
0

您需要删除从arrProjects对象也一样,里面UITableViewCellEditingStyleDelete,

[arrProjects removeObjectAtIndex:indexPath.row]; 

它也是很好包括

[self.tableView beginUpdates] and [self.tableView endUpdates] 

,当你开始和结束去除这些对象

+0

试过这个但不工作。我正在更新有问题的代码。 – Nitish

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

    return [arrProjects count]; 
} 


- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSLog(@""); 
    if (editingStyle == UITableViewCellEditingStyleDelete) 
    { 
     Project *project = [arrProjects objectAtIndex:indexPath.row]; 
     [[CommonModel shared]DeleteProjectDetails:project.ProjectId]; 
     [arrProjects removeObject:project.ProjectId]; 
     [self reloadTableProject: YES]; 
    } 

}