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

时间:2014-10-09 11:41:16

标签: ios objective-c iphone ios7 uicollectionview

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

这是代码: -

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

感谢。

2 个答案:

答案 0 :(得分: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++;


}



}

答案 1 :(得分:0)

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

此方法询问一个特定部分中有多少个单元格。当你在这里返回总是相同的值时,你的部分中将始终有相同数量的单元格。