ios - How to get rid of specific TableView Cells -


my tableview populated cells show item name, price, , brand. objects web service return , , looks ugly on table view cell. don't want populate table view cells have price of "null". here code far. now, change "price unavailable".

   (nsdictionary *objitem in resultsarray) {      nsstring *currentitemname = [objecttitlesarray objectatindex:indexpath.row];      if ([currentitemname isequaltostring:objitem[@"title"]])     {         if (cell.pricelabel.text != (id)[nsnull null] && cell.pricelabel.text.length != 0 && cell.brandlabel.text != (id)[nsnull null] && cell.brandlabel.text.length != 0)         {           cell.namelabel.text = currentitemname;              nsdictionary *bestpagedictionary = objitem[@"best_page"];              nsnumber *price = bestpagedictionary[@"price"];              if ((cell.pricelabel.text = @"<null>"))             {                 cell.pricelabel.text = @"price unavailable";             }              else             {              cell.pricelabel.text = [nsstring stringwithformat:@"$%@", price];             }               nsarray *brandsarray = objitem[@"brands"];         cell.brandlabel.text = [brandsarray firstobject];         }      } } 

this terribly inefficient. keeping json (i'm assuming) return in dictionary, looping on dictionary every cell you're creating. not that, aren't cleaning json ahead of time.

it's more expensive create useless cell go , try delete it. in numberofrowsinsection delegate, you've told tableview have x many cells. you're trying delete cells mess callbacks. you'd have create method run after finish creating cells loop through cells delete them tableview, call [table reloaddata]. however, because aren't removing data nsdictionary, create same amount of cells again , stuck in infinite loop.

solution:

first, change structure. once json return, sanitize delete values no price. suggest using object class hold each server object. simplify code lot tableview other classes well. you've cleaned return, change nsdictionary nsmutablearray. in numberofrowsinsection: call [array count]. in cellforrowatindexpath: @ [array objectatindex:indexpath.row] object.

you'll want code like:

serveritem.h : nsobject{   @property (nonatomic,retain) nsstring* name;   ...   add in other properties here }   - (nsmutablearray *) parsejson:(nsdictionary *)jsondict{   nsmutablearray *returnarray = [nsmutablearray array];   nsarray *dictarray = [nsarray arraywitharray:jsondict[@"results"]];   (nsdictionary *itemdict in dictarray)   {     nsdictionary *bestpagedictionary = objitem[@"best_page"];     if (![bestpagedictionary[@"price"] isequaltostring:@"<null>"]])     {       serveritem item = [serveritem new];       item.price = ...       item.name = ....       [returnarray addobject:item];     }          }   return returnarray; } 

inside webservice code:

self.itemarray = [self parsejson:datadictionary]; 

then

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return [self.itemcallarray count]     }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {   ...   serveritem *cellitem = [self.itemsarray objectatindex:indexpath.row];   cell.namelabel.text = cellitem.name;   cell.pricelabel.text = cellitem.price;   ...  } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -