【发布时间】:2024-01-17 03:27:01
【问题描述】:
使用UICollectionView,我正在尝试添加另一个UICollectionViewCell。第一个代码显示了第一个 Cell,它运行良好。当我尝试添加另一个 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”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]”
我怀疑这与indexPath.row 有关。
如果我将以下代码中的indexPath.row 部分替换为“0”,则它不会崩溃,但不会在UICollectionView 中可见:
[addNew objectAtIndex:indexPath.row]]
【问题讨论】:
标签: ios ios6 uicollectionview uicollectionviewcell