ios - UICollectionView Not Showing When Using IB -
i'm having issues making uicollectionview in app. use ib instead of storyboards. @ bottom of question code .h , .m. in ib, add uicollectionviewcontroller , tell class obj-c class made it. however, when run it, black view.
.h:
#import <uikit/uikit.h> @class webviewcontroller; @class webviewcontroller2; @interface gridlayout : uicollectionviewcontroller <uicollectionviewdatasource, uicollectionviewdelegate> { nsoperationqueue *_queue; nsarray *_feeds; nsmutablearray *_allentries; webviewcontroller *_webviewcontroller; uiactivityindicatorview *activity; webviewcontroller2 *_webviewcontroller2; iboutlet uibarbuttonitem *redo; iboutlet uinavigationbar *mynavigationbar; nsstring *theurl; } -(ibaction)sendtofriend; -(ibaction)refreshing; @property (nonatomic, retain) nsstring *theurl; @property (retain) nsoperationqueue *queue; @property (retain) nsarray *feeds; @property (retain) nsmutablearray *allentries; @property (retain) webviewcontroller *webviewcontroller; @property (retain) uiactivityindicatorview *activity; @property (retain) webviewcontroller2 *webviewcontroller2; @end
.m:
#import "gridlayout.h" #import "rssentry.h" #import "asihttprequest.h" #import "gdataxmlnode.h" #import "reachability.h" #import "gdataxmlelement-extras.h" #import "nsdate+internetdatetime.h" #import "nsarray+extras.h" #import "webviewcontroller.h" #import "settingsviewcontroller.h" #import "webviewcontroller2.h" #import "cell.h" #import "asinetworkqueue.h" #import "uiimageview+webcache.h" @implementation gridlayout @synthesize allentries = _allentries; @synthesize feeds = _feeds; @synthesize queue = _queue; @synthesize webviewcontroller = _webviewcontroller; @synthesize activity; @synthesize webviewcontroller2 = _webviewcontroller2; - (void)refresh { self.allentries = [nsmutablearray array]; self.queue = [[[nsoperationqueue alloc] init] autorelease]; self.feeds = [nsarray arraywithobjects:@"http://feeds.feedburner.com/ipreacherblog", nil]; (nsstring *feed in _feeds) { nsurl *url = [nsurl urlwithstring:feed]; asihttprequest *request = [asihttprequest requestwithurl:url]; [request setdelegate:self]; [_queue addoperation:request]; } } - (void)viewdidload { [super viewdidload]; [activity startanimating]; self.title = @"ipreacher"; uibarbuttonitem *sharing = [[uibarbuttonitem alloc] initwithtitle:@"sharing settings" style:uibarbuttonitemstyleplain target:self action:@selector(gotosettings)]; self.navigationitem.rightbarbuttonitem = sharing; // // create header view. wrap in container allow position // better. // [self refresh]; } -(void)gotosettings { settingsviewcontroller *dvcontroller = [[settingsviewcontroller alloc] initwithnibname:@"settingsviewcontroller" bundle:[nsbundle mainbundle]]; [self.navigationcontroller pushviewcontroller:dvcontroller animated:yes]; } -(void) refreshinvoked:(id)sender forstate:(uicontrolstate)state { // refresh table here... [_allentries removeallobjects]; [self refresh]; } - (void)parserss:(gdataxmlelement *)rootelement entries:(nsmutablearray *)entries { nsarray *channels = [rootelement elementsforname:@"channel"]; (gdataxmlelement *channel in channels) { nsstring *blogtitle = [channel valueforchild:@"title"]; nsarray *items = [channel elementsforname:@"item"]; (gdataxmlelement *item in items) { nsstring *articletitle = [item valueforchild:@"title"]; nsstring *articleurl = [item valueforchild:@"link"]; nsstring *articledatestring = [item valueforchild:@"pubdate"]; nsdate *articledate = [nsdate datefrominternetdatetimestring:articledatestring formathint:dateformathintrfc822]; nsstring *articleimage = [item valueforchild:@"content:encoded"]; nsscanner *thescanner; nsstring *gt =nil; thescanner = [nsscanner scannerwithstring:articleimage]; // find start of tag [thescanner scanuptostring:@"alt=\"\" width=" intostring:null] ; // find end of tag [thescanner scanuptostring:@"/>" intostring:>] ; // replace found tag space //(you can filter multi-spaces out later if wish) nsstring *test = [articleimage stringbyreplacingoccurrencesofstring:[ nsstring stringwithformat:@"%@", gt] withstring:@"alt=\"\" width=\"device-width\" height=\"150\""]; nsstring *comments = [articleurl stringbyappendingstring:@"#respond"]; nsstring *commentslink = [nsstring stringwithformat: @"<a href=\"%@\">leave comment</a>",comments]; nsstring *final = [test stringbyreplacingoccurrencesofstring:@"float:none;height:30px" withstring:@"float:none;height:1px"]; nsdateformatter * dateformatter = [[[nsdateformatter alloc] init] autorelease]; [dateformatter settimestyle:nsdateformattershortstyle]; [dateformatter setdatestyle:nsdateformattermediumstyle]; nsstring *dateofarticle = [dateformatter stringfromdate:articledate]; nsstring *bodyoftext = [[[[[[@"<head><style type='text/css'>a > img {pointer-events: none;cursor: default;}</style></head><b><font size=5><div align=\"left\">" stringbyappendingstring:articletitle] stringbyappendingstring:@"</font></b><font size=3><p style=\"color:#989898\">"] stringbyappendingstring:dateofarticle] stringbyappendingstring:@"</div></p>"] stringbyappendingstring:final] stringbyappendingstring:commentslink]; rssentry *entry = [[[rssentry alloc] initwithblogtitle:blogtitle articletitle:articletitle articleurl:articleurl articledate:articledate articleimage:bodyoftext] autorelease]; [entries addobject:entry]; } } } - (void)parseatom:(gdataxmlelement *)rootelement entries:(nsmutablearray *)entries { nsstring *blogtitle = [rootelement valueforchild:@"title"]; nsarray *items = [rootelement elementsforname:@"entry"]; (gdataxmlelement *item in items) { nsstring *articletitle = [item valueforchild:@"title"]; nsstring *articleurl = [item valueforchild:@"link"]; nsstring *articledatestring = [item valueforchild:@"pubdate"]; nsdate *articledate = [nsdate datefrominternetdatetimestring:articledatestring formathint:dateformathintrfc822]; nsstring *articleimage = [item valueforchild:@"content:encoded"]; nsdateformatter * dateformatter = [[[nsdateformatter alloc] init] autorelease]; [dateformatter settimestyle:nsdateformattershortstyle]; [dateformatter setdatestyle:nsdateformattermediumstyle]; nsstring *dateofarticle = [dateformatter stringfromdate:articledate]; nsstring *bodyoftext = [[[[[@"<b><font size=5><div align=\"left\">" stringbyappendingstring:articletitle] stringbyappendingstring:@"</font></b><font size=3><p style=\"color:#989898\">"] stringbyappendingstring:dateofarticle] stringbyappendingstring:@"</div></p>"] stringbyappendingstring:articleimage]; rssentry *entry = [[[rssentry alloc] initwithblogtitle:blogtitle articletitle:articletitle articleurl:articleurl articledate:articledate articleimage:bodyoftext] autorelease]; [entries addobject:entry]; } } - (void)parsefeed:(gdataxmlelement *)rootelement entries:(nsmutablearray *)entries { if ([rootelement.name compare:@"rss"] == nsorderedsame) { [self parserss:rootelement entries:entries]; } else if ([rootelement.name compare:@"feed"] == nsorderedsame) { [self parseatom:rootelement entries:entries]; } else { nslog(@"unsupported root element: %@", rootelement.name); } } - (void)requestfinished:(asihttprequest *)request { [_queue addoperationwithblock:^{ nserror *error; gdataxmldocument *doc = [[gdataxmldocument alloc] initwithdata:[request responsedata] options:0 error:&error]; if (doc == nil) { nslog(@"failed parse %@", request.url); } else { nsmutablearray *entries = [nsmutablearray array]; [self parsefeed:doc.rootelement entries:entries]; [[nsoperationqueue mainqueue] addoperationwithblock:^{ (rssentry *entry in entries) { int insertidx = [_allentries indexforinsertingobject:entry sortedusingblock:^(id a, id b) { rssentry *entry1 = (rssentry *) a; rssentry *entry2 = (rssentry *) b; return [entry1.articledate compare:entry2.articledate]; }]; [_allentries insertobject:entry atindex:insertidx]; } }]; } }]; } - (void)requestfailed:(asihttprequest *)request { nserror *error = [request error]; nslog(@"error: %@", error); [self refresh]; } -(nsinteger)numberofsectionsincollectionview:(uicollectionview *)collectionview { return 1; } - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section{ return [_allentries count]; } - (uicollectionviewcell *)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ static nsstring *identifier = @"cell"; uicollectionviewcell *cell = [collectionview dequeuereusablecellwithreuseidentifier:identifier forindexpath:indexpath]; rssentry *entry = [_allentries objectatindex:indexpath.row]; nsstring *substring = @"http://316apps.com/ipreachersblog/wp"; nsrange textrange = [entry.articleimage rangeofstring:substring]; uiimageview *recipeimageview = (uiimageview *)[cell viewwithtag:100]; if(textrange.location != nsnotfound){ nsstring *thearticleimage = entry.articleimage; nsregularexpression *expression = [nsregularexpression regularexpressionwithpattern:@"src=\"([^\"]+)\"" options:nsregularexpressioncaseinsensitive error:null]; nsstring *somestring = thearticleimage; nsstring *oneurl = [somestring substringwithrange:[expression rangeoffirstmatchinstring:somestring options:nsmatchingcompleted range:nsmakerange(0, [somestring length])]]; nsstring *finalstring = [oneurl stringbyreplacingoccurrencesofstring:@"src=\"" withstring:@""]; nsstring *thefinalstring = [finalstring stringbyreplacingoccurrencesofstring:@"\"" withstring:@""]; nsdateformatter * dateformatter = [[[nsdateformatter alloc] init] autorelease]; [dateformatter settimezone:[nstimezone localtimezone]]; [dateformatter settimestyle:nsdateformattershortstyle]; [dateformatter setdatestyle:nsdateformattershortstyle]; recipeimageview.image = [uiimage imagenamed:thefinalstring]; recipeimageview.backgroundcolor = [uicolor clearcolor]; } else { nsdateformatter * dateformatter = [[[nsdateformatter alloc] init] autorelease]; [dateformatter settimezone:[nstimezone localtimezone]]; [dateformatter settimestyle:nsdateformattershortstyle]; [dateformatter setdatestyle:nsdateformattershortstyle]; recipeimageview.image = [uiimage imagenamed:@"icon@2x.png"]; recipeimageview.backgroundcolor = [uicolor clearcolor]; } cell.backgroundview = [[uiimageview alloc] initwithimage:[uiimage imagenamed:@"photo-frame.png"]]; return cell; } @end
update: uploaded entire project here.
in gridviewclass, need call reloaddata on collection view in requestfinished method:
- (void)requestfinished:(asihttprequest *)request { [_queue addoperationwithblock:^{ nserror *error; gdataxmldocument *doc = [[gdataxmldocument alloc] initwithdata:[request responsedata] options:0 error:&error]; if (doc == nil) { nslog(@"failed parse %@", request.url); } else { nsmutablearray *entries = [nsmutablearray array]; [self parsefeed:doc.rootelement entries:entries]; [[nsoperationqueue mainqueue] addoperationwithblock:^{ (rssentry *entry in entries) { int insertidx = [_allentries indexforinsertingobject:entry sortedusingblock:^(id a, id b) { rssentry *entry1 = (rssentry *) a; rssentry *entry2 = (rssentry *) b; return [entry1.articledate compare:entry2.articledate]; }]; [_allentries insertobject:entry atindex:insertidx]; } [self.collectionview reloaddata]; // <== missing }]; } }]; }
Comments
Post a Comment