【问题标题】:UICollectionView insertItemAtIndexPath not workingUICollectionView insertItemAtIndexPath 不起作用
【发布时间】:2014-06-22 21:19:56
【问题描述】:

我正在使用 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]]];
}

有什么帮助吗?

【问题讨论】:

  • 在插入 UICollectionView 之前是否在数组中添加对象?
  • 如果您的意思是在数据数组中添加一个对象,那么不,我现在没有数据数组。我在cellForItemAtIndexPath 中添加静态文本。

标签: ios objective-c ios6 uicollectionview


【解决方案1】:

嗯,

首先让我们考虑这种方法,

- (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;
}

在你的类接口中声明 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 中不要分配初始化任何类型的视图并添加为单元格上的子视图,如果您想要在单元格上自定义视图(如图像视图、按钮等),则此代码每次都会在单元格上创建 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 关于收藏视图。我建议阅读它。 祝你好运。

【讨论】:

  • 但是我暂时没有数据源。我只是在cellForItemAtIndexPath 中设置静态文本。所以这意味着我不能插入一个单元格?
  • 好吧,您能否更新您的问题并发布您已实现的所有数据源方法(cellForItem、numberOfItems ..)?
猜你喜欢
  • 2018-03-26
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多