문제

iPad에서 버튼 클릭 (iPad)에서 UICollectionViewCell 데이터를 변경하려고 시도하고 있습니까? 이를 달성하는 방법? Button Click (

on button click on button click on butten click on butten colick.com으로 다른 array count를 보내는 방법

numberOfItemsInSection :

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

그런 다음 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;
}
.

그 다음 버튼 클릭 - (4-5 개의 버튼이 있으므로 CollectionViewMethods에서 각 버튼의 태그를 수집하고 다음과 같이 NSInteger에서 해당 속성을 사용하고 있습니다

-(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]'
.

도움이 되었습니까?

해결책

i Ibaction을 통해 UICollectionView에서 데이터를 추가 / 제거하는 방법을 단순히 묻는 것입니다.UICollectionView's DataSource 및 Reload에 개체를 추가 할 수 있습니다.다음은 뭔가를 추가하는 예제입니다.

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

다른 팁

잘 두 개의 배열을 bool과 함께 유지하고 탭을 기반으로 UICollectionView를 먼저 두 개의 배열 유지

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

처음으로 원래 데이터를 데이터 소스로 설정

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

    isChangeData = false;

    return [_collectionViewImages1 count];
     }

else{

    isChangeData = TRUE;

    return [_collectionViewImages count];
}

}
.

버튼 조치가 BOOL을 true로 만들고 다시로드 UICollectionView

-(IBAction)changeData:(id)sender {

 [self.CollectionView reloadData];
}
.

실수를 한 경우 Pls가 올바르게 해결되기를 바랍니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top