ios - Changing a Table View Controller to a Collection View Controller -
i have table view controller , want change collection view controller, app gets information using json.
i created collection view controller in storyboard. view controller called "upcomingreleasesviewcontroller" , have uicollectionviewcell called "upcomingreleasecell". in storyboard have label linked collection cell called "release_name".
i want transfer code had in tvc i'm having trouble updating it.
the code added upcomingreleasesviewcontroller.h (like had in tvc)
@interface upcomingreleasesviewcontroller : uicollectionviewcontroller @property (strong, nonatomic) nsmutablearray *upcomingreleases; @end i added code upcomingreleasesviewcontroller.m (i error when call cell.textlabel.text)
- (void)viewdidload { [super viewdidload]; nsurl *upcomingreleaseurl = [nsurl urlwithstring:@"http://obscure-lake-7450.herokuapp.com/upcoming.json"]; nsdata *jsondata = [nsdata datawithcontentsofurl:upcomingreleaseurl]; nserror *error = nil; nsdictionary *datadictionary = [nsjsonserialization jsonobjectwithdata:jsondata options:0 error:&error]; self.upcomingreleases = [nsmutablearray array]; nsarray *upcomingreleasesarray = [datadictionary objectforkey:@"upcoming_releases"]; (nsdictionary *upcomingreleasedictionary in upcomingreleasesarray) { upcomingrelease *upcomingrelease = [upcomingrelease upcomingreleasewithname:[upcomingreleasedictionary objectforkey:@"release_name"]]; [self.upcomingreleases addobject:upcomingrelease]; } } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { static nsstring *identifier = @"cell"; uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:identifier forindexpath:indexpath]; upcomingrelease *upcomingrelease = [self.upcomingreleases objectatindex:indexpath.row]; cell.textlabel.text = upcomingrelease.release_name; return cell; } also in when using tvc had nsobject called "upcomingrelease" following code:
upcomingrelease.h
@interface upcomingrelease : nsobject @property (nonatomic, strong) nsstring *release_name; // designated initializer - (id) initwithtitle:(nsstring *)release_name; + (id) upcomingreleasewithname:(nsstring *)release_name; @end upcomingrelease.m
@implementation upcomingrelease - (id) initwithtitle:(nsstring *)release_name { self = [super init]; if ( self ){ self.release_name = release_name; } return self; } + (id) upcomingreleasewithname:(nsstring *)release_name { return [[self alloc] initwithtitle:release_name]; } @end should create nsobject new app , add code or should add upcomingreleasecell?
thanks.
have register custom cell?
[self.collectionview registerclass:[upcomingreleasecell class] forcellwithreuseidentifier:@"upcomingreleasecell"];
Comments
Post a Comment