【发布时间】:2015-09-09 00:23:47
【问题描述】:
UICollectionView 尝试滚动到无效的索引路径 {length = 2, path = 0 - 0} 时发生崩溃。
这是否意味着我的集合视图大小为 2,但我试图滚动到第 0 节索引 0?在什么情况下那不是有效的卷轴?
我们已经消除了使用 NSFetchedResultsController 可能导致崩溃的不必要的复杂性,但我仍然对错误消息的读取方式及其含义感到好奇?
这是代码
void fetch() {
// get data from core data using a FetchRequest
do a perform fetch to get my item
// loop through to see which index the item I want is at
loop through result {
if (my id matches core data id) {
self.selectedIndex = x;
}
}
// get more from core data using a Fetch Request
create another FetchedResultsController
do another fetch to get all other items
[self.myCV reloadData];
// find the index path for the item I want to scroll to
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
[self.myCV scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally | UICollectionViewScrollPositionCenteredVertically animated:NO];
}
// UICollectionView delegate methods
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
id<NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [secInfo numberOfObjects];
}
【问题讨论】:
-
附上你正在使用的一段代码
-
"length" 是
NSIndexPath的长度。通常(如常用),它的长度是两个,一个值用于section,另一个用于row(在UITableView,在UICollectionView),但实际上你可能有更复杂的NSIndexPath,如AppleNSIndexPath的文档。您的问题在其他地方并且没有代码,很难找到原因。 -
myCV的项数在哪里设置? -
@Larme 查看我的编辑。
标签: ios objective-c uicollectionview nsfetchedresultscontroller