iOS uitableview在reloadData时冻结

时间:2015-12-06 09:56:58

标签: ios iphone uitableview reloaddata

我使用nsarray来存储所有的tableview单元格,在我更新了单元格之后,我调用了[tableView reloadData];然后应用程序将冻结。我该如何防止这种情况?

=============================================== ==========================

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    NSUInteger nodeCount = _newsFeeds.count;
    if (cell == nil) {
        CustomCell *customCell = [customCells objectAtIndex:indexPath.row - 1];
        [customCell setDelegate:self];
        [customCell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [customCell displayImages];
        cell = customCell;
    }
    return cell;
}


- (void)getData {
    [[BackendUtil shareInstance] getData];
}

- (void)updateData:(NSNotification *)notification {
    NSDictionary *userInfo  = notification.userInfo;
    NSMutableArray<Data *> *listOfData = [userInfo objectForKey:@"data"];
    _listOfData = listOfData;

    [self createCustomCell:listOfData completion:^(NSArray *arr) {
        customCells = [arr mutableCopy];
        [_tableView reloadData];
    }];
}

- (void)createCustomCell:(NSMutableArray<Data *> *)listOfData completion:(void(^)(NSArray *arr))completion {
    NSMutableArray *dummyArr = [[NSMutableArray alloc] init];
    for (int i = 0 ; i < [data count] ; i++) {
        Data *data = [listOfData objectAtIndex:i];
        CustomCell *customCell = [[CustomCell alloc] initWithFrame:CGRectMake(posX, posY, width, height) data:data];
        [dummyArr addObject:customCell];
    }
    completion(dummyArr);
}

1 个答案:

答案 0 :(得分:0)

CustomCell *customCell = [customCells objectAtIndex:indexPath.row - 1];

为什么要在这里减少indexPath的行? IndexPaths已经从零开始。当调用此行以获取您的桌面视图的第一行时,该行将向您的NSArray发送一个负面索引,这反过来会使您的应用程序崩溃。