gpt4 book ai didi

ios - 最初使用非零值调用的 numberOfRowsInSection

转载 作者:行者123 更新时间:2023-11-29 01:05:20 27 4
gpt4 key购买 nike

当第一次调用以下数据源时

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.firstFetchedResultController.sections.count + self.secondFetchedResultController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rowsCount = 0;
NSLog(@"Section : %ld", (long)section);

//First result controller
if (section < [self.secondFetchedResultController.sections count]) {
id sectionInfo = [self.firstFetchedResultController sections][section];
rowsCount = [sectionInfo numberOfObjects];
} else { //Second result controller
id sectionInfo = [self.secondFetchedResultController sections][section];
rowsCount = [sectionInfo numberOfObjects];
}

return rowsCount;
}

最初打印的部分值为 3。当它进入 numberOfRows 时,应用程序崩溃,因为循环会将进程带到 else block ,而我的 secondaryFetchedResultController 中没有第三部分。

仅供引用:我在当前 ViewController 中使用了两个 fetchedResultsController。我正面临崩溃,因为在这种情况下,第一个/第二个ViewController 对于第 3 节没有值(value)。

最佳答案

您需要将第一个 resultController 的计数减去该部分,以便拥有从 0 开始的索引:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

NSInteger rowsCount = 0;
NSLog(@"Section : %ld", (long)section);

// Let's save the count to a variable in order to have an answer more readable
NSInteger firstSectionCount = [self.secondFetchedResultController.sections count];

//First result controller
if (section < firstSectionCount) {
id sectionInfo = [self.firstFetchedResultController sections][section];
rowsCount = [sectionInfo numberOfObjects];
} else { //Second result controller
// HERE, we subtract firstSectionCount to section.
id sectionInfo = [self.secondFetchedResultController sections][section - firstSectionCount];
rowsCount = [sectionInfo numberOfObjects];
}

return rowsCount;
}

关于ios - 最初使用非零值调用的 numberOfRowsInSection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36467284/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com