我正在尝试更改UICollectionViewCell
按钮单击上的数据(在iPad中)?如何实现呢?如何发送不同的array count
,以numberOfItemsInSection
按钮点击?
在我的viewDidLoad
:
self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil] ;
然后在CollectionViewMethods
:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return [_collectionViewImages count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *myCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
forIndexPath:indexPath];
NSString *collectionViewImageName=[self.collectionViewImages objectAtIndex:indexPath.row];
UIImageView * collectionImage = (UIImageView*)[myCell viewWithTag:11];
collectionImage.image =[UIImage imageNamed:collectionViewImageName];
return myCell;
}
然后在ButtonClick上-(因为有4-5个按钮,我正在收集每个按钮的标签NSInteger
并numberOfItemsInSection
像这样使用该属性,
-(void)btnTapped:(id)sender{
int tag= [(UIButton *)sender tag];
NSLog(@"Button Pressed%d",tag);
_buttonIndex=tag;
//_buttonIndex is NSInteger property
//If i add object in NSMutableArray and call reloadData my UI hangs here only.
//If I empty my NSMutableArray and add new objects in it and then call reloadData, I am getting NSRangeException.
}
现在我将这个属性numberOfItemsInSection
用作...
if (_buttonIndex==1){
[collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
//My _array1 has 4 objects and my collectionViewImages array has 7 objects.
}
我正在收到错误消息
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
我相信您只是在问如何通过IBAction从UICollectionView添加/删除数据。您只需将一个对象添加到您的UICollectionView's
数据源并重新加载。以下是添加内容的示例:
-(IBAction)addObject:(id)sender {
[self.objectArray addObject:@"new object"];
[self.myCollectionView reloadData];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句