Reputation: 127
In my controller, I parse data from a JSON array and use it to populate a collection view. The code below successfully loads and displays the data, but fails to create individual sections.
As such, how do I modify the code to create sections for each object within the array. For example, if my array has a count of 50 urls, how do I create one section for each?
let lastItem = self.photos.count
self.photos.addObjectsFromArray(photoInfos)
let indexPaths = (lastItem..<self.photos.count).map { NSIndexPath(forItem: $0, inSection: 0) }
dispatch_async(dispatch_get_main_queue()) {
self.collectionView!.insertItemsAtIndexPaths(indexPaths)
}
Upvotes: 0
Views: 1420
Reputation: 249
Here is the code that worked for me create the header cell. To do which I created a custom cell class and a nib to do the customization of the cell in the graphic editor
In viewDidLoad
self.collectionView?.registerNib(UINib(nibName: "KlosetCollectionHeaderViewCell", bundle: nil), forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell")
Then you add the delegate function
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> KlosetCollectionHeaderViewCell {
println("entring viewforsuppplementaryElementofKind")
var headerCell = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HeaderCell", forIndexPath: indexPath) as? KlosetCollectionHeaderViewCell
return headerCell!
}
This will put the HeaderCell in the SectionView
of the PFCollectionView
The controls that show in the cell you add them to the xib file as well as the outlets and actions
Upvotes: 0
Reputation: 432
here is my code is in Objective-C, I hope it'll helpful to you.
Here HeaderView
is sub class of UICollectionReusableView
.
Where you can set view as you like.
-(CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(320.0f, 32.0f);
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
HeaderView *view=(HeaderView *)[historyCollection dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
NSArray *viewsToRemove = [view subviews];
for (UIView *v in viewsToRemove) {
[v removeFromSuperview];
}
view.backgroundColor=[UIColor clearColor];
self.month = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 21)];
self.month.text =[[monthList objectAtIndex:indexPath.section] uppercaseString];
self.month.textColor = [UIColor greenColor];
self.month.font=[UIFont boldSystemFontOfSize:17];
[view addSubview:self.month];
return view;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 0;
}
else if (section == 1) {
return 40.0;
}
else {
return 50;
}
}
Upvotes: 1
Reputation: 4163
Most of the time the apps use one section and many items in this section so that's why it may be a bit harder to find how to do your way.
It's all about how you use this 2 functions of the datasource:
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return self.items.count
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
Normally you return 1 section and the array count in the numberOfItemsInSection, but if you switch them you would have exactly what you want.
Upvotes: 1