섹션 기반 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);
}
감사합니다.
정적이 될 경우 다른 것을 사용하고 싶지 않습니다. 동적으로 만들고 싶습니다. – Rushabh
@ 루샤브 (Rushabh) 나는 이것을 좀 더 글로벌하게 만들기 위해 이것을 편집했다. 어쩌면 더 좋을 수도있다. – hoya21