我正在使用 UICollectionView 生成一个单元格网格,假设总共有 10 个单元格,即 0-9。
现在,我想通过单击其中一个单元格在网格中插入一个新单元格。
所以我在函数 didSelectItemAtIndexPath
中添加了以下代码行 [_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:10 inSection:0]]];
。
现在,如果我将 indexPathForItem:
设置为 10(即最后插入),那么我会在该行收到“断言失败”错误。如果我将“indexPathForItem:”设置为 0-9 之间的任何值,那么我会在该行收到“EXC_BAD_ACCESS...”错误。
这是我实现 UICollectionView 的完整代码:
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
_collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 97.5, self.view.frame.size.width, self.view.frame.size.height-67.5) collectionViewLayout:layout];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[_collectionView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:_collectionView];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return 35;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.layer.borderWidth=.5f;
cell.layer.borderColor=[UIColor blackColor].CGColor;
if(indexPath.item<31)
{
_dayNumber = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 15, 15)];
_dayNumber.font = [UIFont systemFontOfSize:12];
_dayNumber.text = [NSString stringWithFormat:@"%ld",(indexPath.item + 1)];
[cell addSubview:_dayNumber];
}
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(self.view.frame.size.width/7, self.view.frame.size.width/7);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex: (NSInteger)section
{
return 0.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 0.0;
}
// Layout: Set Edges
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout: (UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0,0,0,0); // top, left, bottom, right
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath: (NSIndexPath *)indexPath
{
[_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]];
}
有什么帮助吗?
最佳答案
嗯,
首先让我们考虑一下这个方法,
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return 35; // returning constant value means that you can't add or remove cells later
}
所以让我把它改成
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
return self.itemsCount;
}
像这样在类接口(interface)中声明 itemsCount 属性
@interface YourClass ()
@property (nonatomic) NSInteger itemsCount;
@end
在loadView或init方法中初始化,
_itemsCount = 35; // or whatever you want, initial count
现在我们可以插入/删除项目,对吧?当我们调用 insertItemAtIndexPaths 时,我们所要做的就是在调用之前更新实际数据,(例如 self.itemsCount++, [self.myItems addObject:newItem] ) 这是您的代码中的更改
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
self.itemsCount++; // updating data
[_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:0]]];
}
最后一件重要的事情,在 cellForItemAtIndexPath 中不要分配初始化任何类型的 View 并在单元格上添加为 subview ,如果你想在单元格上自定义 View (如 ImageView 、按钮等),此代码每次都会在单元格上创建 UILabel ..) 你应该子类化 UICollectionViewCell 并在它的 init 方法中创建这些东西,这是它的样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
YourCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.layer.borderWidth = 0.5;
cell.layer.borderColor = [UIColor blackColor].CGColor;
cell.dayNumber.font = [UIFont systemFontOfSize:12];
cell.dayNumber.text = [NSString stringWithFormat:@"%d",(indexPath.row + 1)];
return cell;
}
假设你也改变了这一行,
[_collectionView registerClass:[YourCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
请注意 YourCell 是 UICollectionViewCell 的子类并且具有属性 dayNumber
Apple 有一个很棒的guide关于 Collection View 。我建议阅读它。 祝你好运。
关于ios - UICollectionView insertItemAtIndexPath 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23491684/