使用UICollectionView
,我試圖添加另一個UICollectionViewCell
。這第一個代碼顯示第一個單元格,它運行良好。當我試圖添加另一個UICollectionViewCell
時會出現問題。UICollectionView - 多個不同的單元格
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]]