ios - UICollectionView is not reloading data properly -
i'm using uicollectionview
display data received remote server , i'm making 2 reloaddata
calls in short time, first 1 show spinner (which uiactivityindicatorview
in uicollectionviewcell
) , second 1 after data retrieved server. store models based on collection view cells created in self.models
nsarray , in cellforitematindexpath
dequeue cells based on model. issue when call reloaddata
after request completed (the second time) collection view seems still in process of creating cells first reloaddata
call , shows first 2 cells , big blank space in place rest of cells should appear.
i wrote logs insights of what's happening , current workflow is:
i'm populating
self.models
model spinner cell , callreloaddata
on uicollectionview.numberofitemsinsection
called, returning[self.models count]
. returned value 2, fine (one cell acts header + second cell 1 uiactivityindicator inside).i'm making request server, response , populate
self.models
new models received remote server (removing model spinner cell keeping header cell). callreloaddata
on uicollectionview instance.numberofitemsinsection
called, returning number of items retrieved remote server + model header cell (let's returned value 21).it's when
cellforitematindexpath
called twice (which value returnednumberofitemsinsection
when called first).
it seems collection view busy first reloaddata
when call method second time. how should tell collection view stop loading cells first reloaddata
call? tried [self.collectionview.collectionviewlayout invalidatelayout]
seemed work issue reappeared, didn't did trick.
some snippets code:
- (void)viewdidload { [super viewdidload]; [ ...... ] self.models = @[]; self.newsmodels = [nsmutablearray array]; [self.collectionview.collectionviewlayout invalidatelayout]; [self buildmodel:yes]; // show loading indicator [self.collectionview reloaddata]; [self updatenewswithblock:^{ dispatch_async(dispatch_get_main_queue(), ^{ [self.collectionview.collectionviewlayout invalidatelayout]; [self buildmodel:no]; [self.collectionview reloaddata]; }); }]; } - (void) buildmodel:(bool)showspinnercell { nsmutablearray *newmodels = [nsmutablearray array]; [newmodels addobject:self.showmodel]; // self.showmodel model cell acts header if ([self.newsmodels count] != 0) { // self.newsmodels populated in [self updatenewswithblock], see below [newmodels addobjectsfromarray:self.newsmodels]; } else if (showspinnercell) { [newmodels addobject:[spinnercellmodel new]]; } self.models = [nsarray arraywitharray:newmodels]; } - (void) updatenewswithblock:(void (^)())block { // here i'm performing request using `afhttpsessionmanager` retrieve // xml data backend, i'm processing , // i'm instantiating newscellmodel objects represents models. [ ..... ] (nsdictionary *item in (nsarray*)responseobject) { newscellmodel *model = [[newscellmodel alloc] init]; model.itemid = [item[@"id"] integervalue]; model.title = item[@"title"]; model.headline = item[@"short_text"]; model.content = item[@"text"]; [self.newsmodels addobject:model]; } block(); }
Comments
Post a Comment