ios - 带有空数组的 UITableView reloadData

标签 ios uitableview

我正在删除 UITableView 中的所有项目。因此,我从中加载 UITableView 的数组的计数 = 0。删除数组中的最后一项后,在重新加载表时,我在 numberofRowInSection 处收到错误。

- (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];
}

这是错误:

'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 (2), 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)numberOfSectionsInTableView:(UITableView *)tableView {
    return [_datasourceArray count];
}

要清空您的 TableView,请执行:

[_datasourceArray removeAllObjects];
[_tableView reloadData];

关于ios - 带有空数组的 UITableView reloadData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476459/

相关文章:

ios - 当我添加自动布局时, View 消失了

ios - 从 Swift 中的字符串中提取整个单词

ios - 当 tableView 的 allowsMultipleSelectionDuringEditing 属性为 YES 时,如何让滑动删除工作?

objective-c - UIRotationGestureRecognizer 旋转有时会达到非常大的值

ios - OneSignal provisionator 工具不工作

ios - 自动下载和安装 iOS 应用程序

ios - CAShapeLayer 问题 - 不在 UITableViewCell 中绘制

iphone - UISearchBar 作为 UITableView 的 subview

ios - 如何在没有外部库的情况下在 UITableView 中缓存图像?

iphone - 如何在分段控件的某一段中加载uitableview?