2014-05-06 24 views
0

我正在使用UICollectionView来产生一个单元格的网格,说总数为10即0-9。UICollectionView insertItemAtIndexPath不工作

现在,我想单击其中一个单元格,在网格中插入一个新单元格。

所以我在函数didSelectItemAtIndexPath的内部添加了以下代码行[_collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:10 inSection:0]]];。所以现在,如果我将indexPathForItem:设置为10(即最后插入),那么我会在这一行上得到'Assertion failure'错误。如果我设置了'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]]]; 
} 

任何帮助吗?

+0

您是否在插入UICollectionView之前在数组中添加对象? – Arpit

+0

如果你的意思是将一个对象添加到数据数组中,那么不,我目前没有数据数组。我在'cellForItemAtIndexPath'中添加静态文本。 – shrishaster

回答

4

好,

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

- (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不分配init任何类型的视图,并添加为单元格上的子视图,这个代码每次创建单元格上的UILabels,如果你想单元格上的自定义视图(如imageview,按钮等),你应该子类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

苹果大约有收集意见很大guide。我建议阅读它。 祝你好运。

+0

但是我现在没有数据源。我只是在'cellForItemAtIndexPath'中设置了静态文本。那意味着我不能插入一个单元格? – shrishaster

+0

好吧,你能否更新你的问题并发布你已经实现的所有数据源方法(cellForItem,numberOfItems ..)? – Seryozha

+0

为我的问题添加了代码。 – shrishaster

相关问题