2015-12-06 83 views
-1

我用nsarray來存儲所有tableview單元格,我更新單元格後,我調用[tableView reloadData];然後,應用程序將被凍結。我怎樣才能防止呢?iOS的uitableview凍結時,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); 
} 
+0

你在'tableview'中加載了哪種類型的數據? –

+0

類似Facebook的新聞提要,許多文字和一些圖像。 –

+0

CustomCell類有一個reuseIdentifier嗎?隨着你的代碼不能工作,因爲你不想重複使用。 (出於某種原因;)) –

回答

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

你爲什麼遞減indexPath這裏的行? IndexPaths已經是從零開始的。當被調用來獲取tableview的第一行時,這行會向你的NSArray發送一個負面的索引,這反過來會導致你的應用程序崩潰。

+0

這是因爲在數組中的這些單元格之前有一個其他自定義單元格 –

+0

@FaiFabio在這種情況下,在執行導致崩潰的非法操作之前,您的代碼在邏輯上不正確。我在您發佈的代碼中看不到任何對此其他自定義單元格的引用。你從哪裏得到它? –

+0

我只是跳過那些不能公開的代碼,現在,我的應用程序不會崩潰。 它將在重新加載tableview時凍結。重新加載過程完成後,它將恢復。 –