-1

I used nsarray to store all the tableview cells, after I updated the cells, I called [tableView reloadData]; Then the app will be freeze. How can I prevent that?

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

- (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);
}
Fai Fabio
  • 19
  • 5

1 Answers1

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

Why are you decrementing the row of indexPath here? IndexPaths are already zero-based. This line will send a negative index to your NSArray when called to get the first row of your tableview, which in turn will crash your app.

Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
  • it is because there is an other custom cell before those cells in the array – Fai Fabio Dec 06 '15 at 11:27
  • @FaiFabio In that case, your code is logically incorrect on top of carrying out an illegal operation that will lead to a crash. I can't see any references to this other custom cell in the code you posted. Where do you get it from? – Şafak Gezer Dec 06 '15 at 11:38
  • I just skip those code that cannot public, and now, my app is not crash. It will be freeze when it is reloading the tableview. After the reload process finish, it will resume. – Fai Fabio Dec 06 '15 at 11:42
  • Then you must be handling the row 0 case in your hidden code, as the code you posted will surely crash. It's hard to guess anything when you're randomly omitting code. If you have a large number of cells, there is a chance that your updateData method takes too long causing the freeze. One bit of advice I can give you is that "preloading" cells by sticking them to an array before displaying is almost never the way to go for populating tableviews. Cells must be created at the time they're needed in the cellForRowAtIndexPath method and caching must be done using the built-in reuse mechanism. – Şafak Gezer Dec 06 '15 at 13:53
  • If you are using more than one cell type in a UITableView you should have cases to handle the specific cell type. This link might be useful for figuring that out 2 different types of custom UITableViewCells in UITableView: http://stackoverflow.com/questions/1405688/2-different-types-of-custom-uitableviewcells-in-uitableview – xdeleon Dec 06 '15 at 18:17
  • @xdeleon you are right in general but since he is (badly) not using cell reusing at all this isn't ok for him. he creates the cells beforehand :) – Daij-Djan Dec 07 '15 at 09:12