Question

I am trying to change UICollectionViewCell data on button click(in iPad)? How to achieve this? How to send different array count to numberOfItemsInSection on button Click?

In my viewDidLoad:

self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil] ;

Then in 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;
}

Then on ButtonClick-- (As there are 4-5 buttons i am collecting tag of each button in NSInteger and using that property in numberOfItemsInSection like this,

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

And now i am using this property in numberOfItemsInSection as...

if (_buttonIndex==1){

     [collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
     //My _array1 has 4 objects and my collectionViewImages array has 7 objects.

    }

i am getting error message as

*** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
Was it helpful?

Solution

I believe you're just simply asking how to add/remove data from a UICollectionView through an IBAction. You would just add an object to your UICollectionView's datasource and reload. Here is an example of adding something:

-(IBAction)addObject:(id)sender {
  [self.objectArray addObject:@"new object"];
  [self.myCollectionView reloadData];
}

OTHER TIPS

well you keep two array with a BOOL and based on taps you relload the UicollectionView first keep two array

Declare BOOL

BOOL isChangeData;

Ex :

 self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil] ;

 self.collectionViewImages1=[[NSMutableArray alloc]initWithObjects:@"4.jpg",@"5.jpg",@"6.jpg",nil] ;

 isChangeData = TRUE;

First time set original Data as Datasource

-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
    if(isChangeData){

    isChangeData = false;

    return [_collectionViewImages1 count];
     }

else{

    isChangeData = TRUE;

    return [_collectionViewImages count];
}

}

in button action Make BOOL true and reload UICOllectionView

-(IBAction)changeData:(id)sender {

 [self.CollectionView reloadData];
}

Hope this Works if i did any mistake pls correct me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top