ios - UICollectionView not loading programatically -
ios - UICollectionView not loading programatically -
i trying hook uicollectionview in application , see black screen every time load it. below code have written:
caller code:
uicollectionviewflowlayout *flowlayout = [[uicollectionviewflowlayout alloc] init]; [flowlayout setitemsize:cgsizemake(200, 200)]; [flowlayout setscrolldirection:uicollectionviewscrolldirectionhorizontal]; myappdashboardhomeviewcontroller *adashboardviewcontroller = [[myappdashboardhomeviewcontroller alloc] initwithcollectionviewlayout:flowlayout]; [self presentviewcontroller:adashboardviewcontroller animated:yes completion:nil];
uicollectioncontroller sub class
#import <uikit/uikit.h> @interface myappdashboardhomeviewcontroller : uicollectionviewcontroller <uicollectionviewdelegate, uicollectionviewdatasource> @end #import "myappdashboardhomeviewcontroller.h" #import "myappdashboardcollectionviewcell.h" #import "myappcustomnavigationbar.h" @interface myappdashboardhomeviewcontroller () @property (nonatomic, strong) nsmutablearray *applications; @property (nonatomic) uiview *containerview; @property (nonatomic, strong) myappcustomnavigationbar *customnavbar; @end @implementation myappdashboardhomeviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view [self.collectionview registerclass:[myappdashboardcollectionviewcell class] forcellwithreuseidentifier:@"myappdashboardcell"]; self.containerview = [[uiview alloc] initwithframe:cgrectmake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height)]; [self.containerview setbackgroundcolor:[uicolor blackcolor]]; [self.containerview setbackgroundcolor:[[uicolor blackcolor] colorwithalphacomponent:0.5]]; [self.view addsubview:self.containerview]; // adding custom navigation bar self.customnavbar = [[myappcustomnavigationbar alloc] initwithtitle:kmyappnewtesttitle detailedtitle:nil informationbardata:nil buttontype:myappmodalscreen anddelegate:self]; [self.containerview addsubview:self.customnavbar]; self.applications = [[nsmutablearray alloc] initwithobjects:@"test 1", @"test 2", @"test 3", @"test 4", nil]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (nsinteger)collectionview:(uicollectionview *)icollectionview numberofitemsinsection:(nsinteger)isection { homecoming self.applications.count; } // cell returned must retrieved phone call -dequeuereusablecellwithreuseidentifier:forindexpath: - (uicollectionviewcell *)collectionview:(uicollectionview *)icollectionview cellforitematindexpath:(nsindexpath *)iindexpath { static nsstring *cellidentifier = @"myappdashboardcell"; myappdashboardcollectionviewcell *acell = (myappdashboardcollectionviewcell *)[icollectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:iindexpath]; if (!acell) { acell = [[myappdashboardcollectionviewcell alloc] init]; } acell.appname = self.applications[iindexpath.row]; homecoming acell; } - (nsinteger)numberofsectionsincollectionview:(uicollectionview *)icollectionview { homecoming 1; } - (void)backbuttonaction:(id)sender { [self dismissviewcontrolleranimated:yes completion:nil]; }
uicollectionviewcell sub class
#import <uikit/uikit.h> @interface myappdashboardcollectionviewcell : uicollectionviewcell @property (nonatomic, strong) nsstring *appname; @end #import "myappdashboardcollectionviewcell.h" @interface myappdashboardcollectionviewcell () @property (nonatomic, strong) uilabel *appnamelabel; @end @implementation myappdashboardcollectionviewcell - (id)init { self = [super init]; if (self) { self.appnamelabel = [[uilabel alloc] initwithframe:cgrectzero]; self.appnamelabel.textalignment = nstextalignmentcenter; self.appnamelabel.textcolor = [uicolor whitecolor]; self.appnamelabel.font = [uifont systemfontofsize:17.0]; self.backgroundcolor = [uicolor redcolor]; [self.viewforbaselinelayout addsubview:self.appnamelabel]; } homecoming self; } - (void)layoutsubviews { [super layoutsubviews]; cgsize atextsize = [self.appname sizewithfont:[uifont systemfontofsize:17.0]]; cgrect appnameframe = cgrectmake((self.frame.size.width - atextsize.width) / 2, (self.frame.size.height - atextsize.height) / 2, atextsize.width, atextsize.height); self.appnamelabel.frame = appnameframe; } @end
the init method uicollectionviewcell should initwithframe: (inherited uiview), think problem plain init method not beingness called.
ios objective-c cocoa-touch uicollectionview uicollectionviewcell
Comments
Post a Comment