ios - Creating a UICollectionView within a subview -
i have view controller (that sits in tab bar controller), need add collection view controller. see lots of tutorials on google seem point creating uicollectionviewcontroller
, starting in viewdidload
. how do in subview?
i have view controller.m file so:
- (void) createview // called viewdidload { uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0.0, 64.0, screenwidth, screenheight)]; [scrollview.layer addgradient]; acollectionview *theview = [[acollectionview alloc] init]; [self.view addsubview:theview]; }
next started uicollectionview
subclass called acollectionview.h
@interface acollectionview : uicollectionview @end
and .m file this:
#import "acollectionview.h" @implementation bestcollectionview - (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self) { // initialization code } return self; }
where start collection view, in initwithframe
?
i trying follow this: creating uicollectionview programmatically
is paradigm correct?
@property (nonatomic, strong) uicollectionview *collectionview; - (void)viewdidload { [super viewdidload]; uicollectionviewflowlayout *layout = [[uicollectionviewflowlayout alloc] init]; self.collectionview = [[uicollectionview alloc] initwithframe:self.yoursubview.frame collectionviewlayout:layout]; [self.collectionview setdatasource:self]; [self.collectionview setdelegate:self]; [self.collectionview registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@"cellidentifier"]; [self.yoursubview addsubview:self.collectionview]; } - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return 50; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { uicollectionviewcell *cell=[collectionview dequeuereusablecellwithreuseidentifier:@"cellidentifier" forindexpath:indexpath]; cell.backgroundcolor = [uicolor redcolor]; return cell; } - (cgsize)collectionview:(uicollectionview *)collectionview layout:(uicollectionviewlayout*)collectionviewlayout sizeforitematindexpath:(nsindexpath *)indexpath { return cgsizemake(100, 100); }
Comments
Post a Comment