使用相当短的照片列表创建新视图/集合视图时会出现问题 – 通常小于30.当我旋转设备时,集合视图会尝试重新渲染,并且应用程序崩溃时出现“索引X超出bounds [0 .. Y]“,其中X是大于Y的数字(数组的最后一个元素).
我注意到,只有当新显示的集合视图的项目少于“根”集合视图时,才会发生这种情况.
以下是相关(部分,无论如何)代码的示例:
- (NSInteger)collectionVIEw:(UICollectionVIEw *)vIEw numberOfItemsInSection:(NSInteger)section { return [[photos objectForKey:API.directoryID] count];}- (NSInteger)numberOfSectionsInCollectionVIEw: (UICollectionVIEw *)collectionVIEw { return 1;}- (UICollectionVIEwCell *)collectionVIEw:(UICollectionVIEw *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { PhotoCell *photoCell = [cv dequeueReusableCellWithReuseIDentifIEr:@"Cell" forIndexPath:indexPath]; photoCell.photo = [[photos objectForKey:API.directoryID] objectAtIndex:indexPath.row]; return photoCell;}
至少,我想对cellForItemAtIndexPath进行try / catch,但不知道我将把try / catch放在哪里.也许在自定义UICollectionVIEw中?
谢谢!
解决方法 问题出在这里:- (NSInteger)collectionVIEw:(UICollectionVIEw *)vIEw numberOfItemsInSection:(NSInteger)section { return [[photos objectForKey:API.directoryID] count];}
我假设您为所有UICollectionVIEw实例使用一个委托和dataSource对象.
如果是这样,请以这种方式重写代码:
- (NSInteger)collectionVIEw:(UICollectionVIEw *)vIEw numberOfItemsInSection:(NSInteger)section{ if ( vIEw == _firstCollectionVIEw ) { return [[photos objectForKey:API.directoryID] count]; } else if ( vIEw == _secondCollectionVIEw ) { return ...; }}
你也应该以这种方式重写collectionVIEw:cellForItemAtIndexPath:方法.
总结以上是内存溢出为你收集整理的ios – 在超出边界范围内调用cellForItemAtIndexPath全部内容,希望文章能够帮你解决ios – 在超出边界范围内调用cellForItemAtIndexPath所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)