Home > DeveloperSection > Forums > UICollectionVIew in iOS
Samuel Fernandes
Samuel Fernandes

Total Post:156

Points:1096
Posted on    October-18-2014 12:21 AM

 iPhone iPhone  Ios  Ios 7 
Ratings:


 1 Reply(s)
 825  View(s)
Rate this:

I am Developing Section based UICollection view something like section based UITablview.but each section have different number of data. but my issue is that it contain the similar data for every section.

Here is Code :-

- (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);

}



Takeshi Okada
Takeshi Okada

Total Post:89

Points:629
Supported
Posted on    October-18-2014 12:22 AM

Something like this could help you.

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

Don't want to miss updates? Please click the below button!

Follow MindStick