Domanda

Sto cercando di cambiare i dati UICollectionViewCell sul pulsante Click (in iPad)? Come raggiungere questo? Come inviare diversi generatoriGoDetagCode a array count On Button Click?

nel mio numberOfItemsInSection:

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

poi in viewDidLoad:

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

Allora su ButtonClick-- (Come ci sono 4-5 pulsanti che sto raccogliendo il tag di ciascun pulsante in CollectionViewMethods e utilizzando quella proprietà in NSInteger come questo,

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

E ora sto usando questa proprietà in numberOfItemsInSection come ...

if (_buttonIndex==1){

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

    }
.

Sto ricevendo un messaggio di errore come

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

È stato utile?

Soluzione

Credo che tu stia semplicemente chiedendo come aggiungere / rimuovere i dati da un uicollectionVisualizza attraverso un iBaction.Aggiungeresti solo un oggetto al tuo UICollectionView's DataSource e Ricarica.Ecco un esempio di aggiungere qualcosa:

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

Altri suggerimenti

Bene, tieni due array con un bool e basato sui rubinetti che hai reldoad the uicollectionView prima di mantenere due array

Dichiarare 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;
.

Prima volta impostare i dati originali come DataSource

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

    isChangeData = false;

    return [_collectionViewImages1 count];
     }

else{

    isChangeData = TRUE;

    return [_collectionViewImages count];
}

}
.

in Pulsante Azione Make BOOL TRUE e Ricarica UICOLLECTIONVEW

-(IBAction)changeData:(id)sender {

 [self.CollectionView reloadData];
}
.

Spero che funzioni se avessi fatto qualche errore, i pls correggono.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top