Como alterar os dados do UICollectionView ao clicar no botão ios?
-
21-12-2019 - |
Pergunta
Estou tentando mudar UICollectionViewCell
dados ao clicar no botão (no iPad)?Como conseguir isso?Como enviar diferente array count
para numberOfItemsInSection
no botão Clique?
No meu viewDidLoad
:
self.collectionViewImages=[[NSMutableArray alloc]initWithObjects:@"1.jpg",@"2.jpg",@"3.jpg",nil] ;
Então em 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;
}
Em seguida, ButtonClick-- (como existem 4-5 botões, estou coletando tag de cada botão em NSInteger
e usando essa propriedade em numberOfItemsInSection
assim,
-(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 agora estou usando esta propriedade em numberOfItemsInSection
como...
if (_buttonIndex==1){
[collectionImage setImage:[UIImage imageNamed:[_array1 objectAtIndex:indexPath.row]]];
//My _array1 has 4 objects and my collectionViewImages array has 7 objects.
}
estou recebendo mensagem de erro como
*** Terminating app due to uncaught exception 'NSRangeException',
reason: '*** -[__NSArrayM objectAtIndex:]: index 4 beyond bounds [0 .. 3]'
Solução
Acredito que você está simplesmente perguntando como adicionar/remover dados de um UICollectionView por meio de um IBAction.Você apenas adicionaria um objeto ao seu UICollectionView's
fonte de dados e recarregar.Aqui está um exemplo de adição de algo:
-(IBAction)addObject:(id)sender {
[self.objectArray addObject:@"new object"];
[self.myCollectionView reloadData];
}
Outras dicas
bem, você mantém dois arrays com um BOOL e com base nos toques você recarrega o UicollectionView primeiro mantém dois arrays
Declarar 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;
Primeira vez, configure os dados originais como fonte de dados
-(NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
if(isChangeData){
isChangeData = false;
return [_collectionViewImages1 count];
}
else{
isChangeData = TRUE;
return [_collectionViewImages count];
}
}
na ação do botão Torne BOOL verdadeiro e recarregue o UICOllectionView
-(IBAction)changeData:(id)sender {
[self.CollectionView reloadData];
}
Espero que funcione, se eu cometi algum erro, por favor, corrija-me.