ios - Load data before UITableViewController will be shown -


i have app, , must info internet. request url in block , made after uiviewcontroller appear, , makes app crash on runtime because use info construct uiviewcontroller.

i don't know how assure code block finish tasks , later use info.

*edit*

now app shows empty table, , dont know how make shows info get. maybe reloadtable, don't know how. help!

storecontroller.m

- (void)viewdidload {     [super viewdidload];     _productdata = [productdata productdata];     [_productdata backendtest]; }  -(nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {     return [_listofchips count]; }  - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { productcellcontroller *cell = (productcellcontroller*)[tableview dequeuereusablecellwithidentifier:simpletableidentifier];             if (cell == nil)             {                 nsstring* namenib = uiuserinterfaceidiompad == ui_user_interface_idiom() ? @"productcellcontroller" : @"productcellcontrolleriphone";                 nsarray *nib = [[nsbundle mainbundle] loadnibnamed:namenib owner:self options:nil];                 cell = [nib objectatindex:0];             }             cell = (productcellcontroller*)[self cellformat:cell attheindex:indexpath withthelastindex:pathtolastrow];             cell.image.image = [uiimage imagenamed:[((chip*)[[_productdata listofproducts] objectatindex:indexpath.row])image]];             cell.title.text = [nsstring stringwithformat:@"%@", [((chip*)[[_productdata listofproducts] objectatindex:indexpath.row])title]];             cell.price.text = [nsstring stringwithformat:@"$ %@", [((chip*)[[_productdata listofproducts] objectatindex:indexpath.row])price]];             cell.amount.text = [nsstring stringwithformat:@"%@", [((chip*)[[_productdata listofproducts] objectatindex:indexpath.row])quantity]]; } 

productdata.m

+(productdata*)productdata {     productdata* data = [[productdata alloc]init];     return data; } - (void)backendtest {      [server getproductswithcompletionhandler:^(id result, nserror* error)      {          if ( error )          {              nslog(@"error %@", error);          }          else          {              //nsmutablearray* arrayproducts = [nsmutablearray array];              int index = 0;              (nsdictionary* product in result)              {                  [_listofproducts addobject:[[products alloc] initwithproduct:[chip objectforkey:gsr_param_product_id]                                                           withtitle:[product objectforkey:gsr_param_product_description]                                                          numberuses:[product objectforkey:gsr_param_product_amount]                                                           withprice:[product objectforkey:gsr_param_product_price]                                                        withquantity:[product objectforkey:gsr_param_product_amount]                                                         withinappid:[product objectforkey:gsr_param_product_in_app_id]                                                           withimage:[[self loadimages]objectatindex:index ]] ];                                      index++;              }          }      }];  } 

i dont have idea how manage block , assure accomplish. ideas? in advance!

what - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section like?

you should returning count of listofproducts.

when completion block done you'll need call reloaddata on tableview. you'll need pass pointer tableview, or add delegate protocol existing code make work.

edit

add delegate protocol allow completion block tell tableview reload. this:

productdata.h

@protocol productdatadelegate;  @interface productdata @property (nonatomic, weak) id<productdatadelegate> delegate; ... @end  @protocol productdatadelegate <nsobject> -(void)didfinishloading; @end 

productdata.m

at end of completion block, once load done:

if (self.delegate != nil) {     [self.delegate didfinishloading]; } 

storecontroller.m

set delegate point storecontroller object.

viewdidload() ... _productdata = [productdata productdata]; _productdata.delegate = self; ... 

and add code reload, assuming storecontroller child class of uitableviewcontroller.

-(void)didfinishloading() {     [self reloaddata]; } 

that should it!


Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -