2014-10-09 2 views
0

섹션 기반 UICollection보기를 개발 중입니다. 섹션 기반 UITablview와 유사하지만 각 섹션의 데이터 수는 다릅니다. 하지만 내 문제는 모든 섹션에 대해 비슷한 데이터가 포함되어 있다는 것입니다. 여기 iOS에서 섹션 기반 UICollectionVIew를 만드는 방법

코드입니다 : -

- (void)viewDidLoad{ 
    [super viewDidLoad]; 

    //registering class for cell reusing 
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"]; 

    //registering class for view reusing 
    [self.collectionView registerClass:[SupplementaryView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SupplementaryView"]; 

    NSDictionary *animals = @{@"B" : @[@"Bear", @"Black Swan", @"Buffalo"], 
       @"C" : @[@"Camel", @"Cockatoo"], 
       @"D" : @[@"Dog", @"Donkey"], 
       @"E" : @[@"Emu"], 
       @"G" : @[@"Giraffe", @"Greater Rhea"], 
       @"H" : @[@"Hippopotamus", @"Horse"], 
       @"K" : @[@"Koala"], 
       @"L" : @[@"Lion", @"Llama"], 
       @"M" : @[@"Manatus", @"Meerkat"], 
       @"P" : @[@"Panda", @"Peacock", @"Pig", @"Platypus", @"Polar Bear"], 
       @"R" : @[@"Rhinoceros"], 
       @"S" : @[@"Seagull"], 
       @"T" : @[@"Tasmania Devil"], 
       @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]}; 

    NSArray* animalSectionTitles = [[animals allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 


} 

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath]; 
    cell.label.text = @"Hello"; 
    cell.backgroundColor = [UIColor darkGrayColor]; 
    return cell; 
} 

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
      return [animals count]; 
} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 
    return [animalSectionTitles count]; 

} 

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ 

    SupplementaryView *supplementaryView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"SupplementaryView" forIndexPath:indexPath]; 

    if(kind == UICollectionElementKindSectionHeader){ 
     supplementaryView.backgroundColor = [UIColor lightGrayColor]; 
     supplementaryView.label.text = @"Header"; 
    } 
    else{ 

    } 

    return supplementaryView; 
} 

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{ 
    return CGSizeMake(500, 50); 
} 

감사합니다.

답변

1

이와 비슷한 것이 도움이 될 수 있습니다.

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 



int i=0; 

for (NSDictionary *item in animals) { 


    if (section==i) { 
     NSArray *lala=[animals objectForKey:item]; 

     NSLog(@"%d",lala.count); 

     return lala.count; 
    } 


    i++; 


} 



} 
+0

정적이 될 경우 다른 것을 사용하고 싶지 않습니다. 동적으로 만들고 싶습니다. – Rushabh

+0

@ 루샤브 (Rushabh) 나는 이것을 좀 더 글로벌하게 만들기 위해 이것을 편집했다. 어쩌면 더 좋을 수도있다. – hoya21

0
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 
      return [animals count]; 
} 

이 방법은 특정 섹션에 얼마나 많은 셀이 있는지 묻습니다. 여기에 돌아올 때 항상 동일한 값이 섹션에 항상 같은 양의 셀이됩니다.

관련 문제