如何在iOS中创建基于节的UICollectionVIew

如何在iOS中创建基于节的UICollectionVIew,ios,objective-c,iphone,ios7,uicollectionview,Ios,Objective C,Iphone,Ios7,Uicollectionview,我正在开发基于节的UICollection视图,类似于基于节的UITableView。但是每个节都有不同数量的数据。但我的问题是,它包含每个部分的相似数据 以下是代码:- - (void)viewDidLoad{ [super viewDidLoad]; //registering class for cell reusing [self.collectionView registerClass:[CollectionViewCell class] forCellWit

我正在开发基于节的UICollection视图,类似于基于节的UITableView。但是每个节都有不同数量的数据。但我的问题是,它包含每个部分的相似数据

以下是代码:-

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

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

此方法询问一个特定部分中有多少单元格。当您返回此处时,始终会有相同的值,分区中的单元格数量也将始终相同。

类似的内容可能会对您有所帮助

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


}



}

我不想使用,否则它会变成静态的,我想变成动态的。@Rushabh我编辑了这个,使它更全球化。现在也许更好。