Ios UICollectionView-多个不同的单元格
使用Ios UICollectionView-多个不同的单元格,ios,ios6,uicollectionview,uicollectionviewcell,Ios,Ios6,Uicollectionview,Uicollectionviewcell,使用UICollectionView,我试图添加另一个UICollectionViewCell。第一个代码显示第一个单元格,它运行正常。当我尝试添加另一个UICollectionViewCell时,会出现此问题 static NSString *identifier = @"Cell"; // Default Cells UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ide
UICollectionView
,我试图添加另一个UICollectionViewCell
。第一个代码显示第一个单元格,它运行正常。当我尝试添加另一个UICollectionViewCell
时,会出现此问题
static NSString *identifier = @"Cell"; // Default Cells
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *illustrationImage = (UIImageView *)[cell viewWithTag:100];
illustrationImage.image = [UIImage imageNamed:[stackImage objectAtIndex:indexPath.row]];
UILabel *stackLabel = (UILabel *)[cell viewWithTag:12];
[stackLabel setText:[stackTitle objectAtIndex:indexPath.row]];
在这里,我想向我的UICollectionView
添加一个全新的单元格,其中包含不同的内容。每当我运行此代码时,整个程序都拒绝启动:
static NSString *newIdentifier = @"AddNewCell";
UICollectionViewCell *newCell = [collectionView dequeueReusableCellWithReuseIdentifier:newIdentifier forIndexPath:indexPath];
UIImageView *addNewImage = (UIImageView *)[newCell viewWithTag:120];
addNewImage.image = [UIImage imageNamed:[addNew objectAtIndex:indexPath.row]]; // This line makes the app crash
return cell;
return newCell;
错误消息
由于未捕获异常“NSRangeException”而终止应用程序,原因:“*-[\uu NSArrayI objectAtIndex:]:索引1超出边界[0..0]”
我怀疑这与indexPath.row
有关。
如果我将以下代码中的indexPath.row
部分替换为“0”,则它不会崩溃,但在UICollectionView
中不可见:
[addNew objectAtIndex:indexPath.row]]
首先,我假设您正在运行在 collectionView:cellForItemAtIndexPath:方法,而不是代码中的其他地方 此外,您不能在所述方法内同时创建(或返回!!)两个单元格。您应该根据作为参数接收到的索引路径来决定退出队列、实例化和返回哪个单元格 现在,关于您收到的“索引超出边界”错误:您正在尝试访问名为
addNew
的数组元素,该元素在您想要访问它时并不实际存在于数组中。在本例中,您尝试访问数组的第二个元素(索引1
),而数组只有一个元素(索引0
)
在访问addNew
数组之前,您可能需要验证该数组是否已正确初始化