简体   繁体   English

调用 UITableView reloadSections 方法崩溃

[英]Call UITableView reloadSections method crashes

I just do the normal operation: Request the data from the server and refresh the tableview by reloadSections , but I do get a lot of crash logs.我只是做正常的操作:从服务器请求数据并通过reloadSections刷新 tableview,但我确实得到了很多崩溃日志。 If I call reloadData , everything is OK.如果我调用reloadData ,则一切正常。 Even though I known reloadData works well, it seems that reloadSections works more efficiently.尽管我知道reloadData运行良好,但reloadSections运行效率似乎更高。
Does anybody have any ideas?有人有任何想法吗?

Get data from the server by persistent connection :通过持久连接从服务器获取数据:

[_pomelo onRoute:@"onTlive" withCallback:^(NSDictionary *data) {
    @strongify(self);
    if (self.liveTableView) {
        [self.liveTableView configData:data refresh:self.playerView.isScreenPortrait];
    }
}];

Then config the data and refresh the tableview:然后配置数据并刷新tableview:

- (void)configData:(NSDictionary *)data refresh:(BOOL)refresh {
if (!data || data.allKeys.count == 0) {
    return;
}
NSArray *liveArray = [data valueForKeyPath:@"body.data"];
[liveArray enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSArray *liveDetailInfo = [obj componentsSeparatedByString:@"|"];
    if (liveDetailInfo && liveDetailInfo.count > 0) {
        NSArray *tliveInfo = [liveDetailInfo.lastObject componentsSeparatedByString:@"^"];
        SPBasketballLive *liveObject = [SPBasketballLive new];
        liveObject.type = tliveInfo[0];
        liveObject.time = tliveInfo[1];
        liveObject.team = tliveInfo[2];
        liveObject.playerId = tliveInfo[3];
        liveObject.score = tliveInfo[4];
        liveObject.text = tliveInfo[5];
        liveObject.quarter = ((NSString *)liveDetailInfo.firstObject).integerValue;
        if (self.curMatch.liveInfo.count == liveObject.quarter) { 
            NSMutableArray *quarterArray  = self.curMatch.liveInfo.firstObject;
            [quarterArray insertObject:liveObject atIndex:0];
        } else { 
            NSMutableArray *quarterArray = [NSMutableArray new];
            [quarterArray addObject:liveObject];
            [self.curMatch.liveInfo insertObject:quarterArray atIndex:0];
        }
    }
}];
if (refresh) {
    if (_curMatch.liveInfo.count > 0) {
        [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:4] withRowAnimation:UITableViewRowAnimationNone];
    }
}
}

Here is the crash log:这是崩溃日志:

invalid update: invalid number of rows in section 4. The number of rows contained in an existing section after the update (98) must be equal to the number of rows contained in that section before the update (96), 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)无效更新:第 4 节中的行数无效。更新后现有节中包含的行数 (98) 必须等于更新前该节中包含的行数 (96),加上或减去该数字从该部分插入或删除的行数(插入 0 行,删除 0 行)加上或减去移入或移出该部分的行数(0 移入,0 移出)

Your app crashed because reloadSection method won't update section and row counters so it causes crashes in cases when you changed section or row counter and your UITableView have different counters.您的应用程序崩溃是因为reloadSection方法不会更新部分和行计数器,因此在您更改部分或行计数器并且UITableView具有不同计数器的情况下,它会导致崩溃。

And when you use reloadData it first call numberOfSectionsInTableView and then numberOfRowsInSection is called so your UITableView has correct counters for sections and rows.而当你使用reloadData它首先调用numberOfSectionsInTableView然后numberOfRowsInSection被称为所以你UITableView有节和行正确的计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM