ios - JSON data to UITableView using Objective-C -
-- updated question main json file --
i'm trying add names table view. table empty. (xcode 7.1b1, ios 9.1b2) sample .json file i'm testing code with. main json file has lot more data.
i successful in adding data sample json file. after using main json file, table empty again.
.json file
[ { "150978": { "jobs": { "d0-c14008-cw": { "05-9032": { "g-05": { "hours_rwk": "false", "hours_st": "4l", "hours_ewo_id": "d0-c14008-cwddda", "hours_ewo": "true", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } }, "05-9031": { "b-02": { "hours_rwk": "false", "hours_st": "4l", "hours_ewo_id": "", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } } } }, "name": "cirillobetancourt", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "", "signed_in": "true", "time_in": "08: 30", "time_out": "16: 30", "signed_out": "true" }, "151243": { "jobs": {}, "name": "jasondiaz", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "151248": { "jobs": {}, "name": "angeltorresmadrigal", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "151336": { "jobs": {}, "name": "danielfrey", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "151419": { "jobs": { "d0-c14009-cw1": { "05-9033": { "g-07": { "hours_rwk": "false", "hours_st": "8l", "hours_ewo_id": "0", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } } }, "d0-c14008-cw": { "05-9031": { "b-02": { "hours_rwk": "false", "hours_st": "0l", "hours_ewo_id": "0", "hours_ewo": "true", "hours_dt": "0l", "hours_ot": "2l", "comments": "" } } } }, "name": "mikegullan", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9990666": { "jobs": {}, "name": "mattstaricha", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9992610": { "jobs": {}, "name": "matthewbigelow", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9992778": { "jobs": {}, "name": "zhoutan", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9993273": { "jobs": {}, "name": "randyfarinelli", "site_id": "d0-c14008-cw", "trade": "carpenter", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9996467": { "jobs": {}, "name": "alvarosandoval(guardado)", "site_id": "d0-c14008-cw", "trade": "mason", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9996763": { "jobs": {}, "name": "jesuszarate", "site_id": "d0-c14008-cw", "trade": "mason", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9996767": { "jobs": {}, "name": "jorgeromero", "site_id": "d0-c14008-cw", "trade": "mason", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9996768": { "jobs": { "d0-c14008-cw": { "05-9032": { "g-05": { "hours_rwk": "false", "hours_st": "3l", "hours_ewo_id": "0", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } }, "05-9031": { "b-02": { "hours_rwk": "false", "hours_st": "5l", "hours_ewo_id": "0", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } } } }, "name": "victorruiz", "site_id": "d0-c14008-cw", "trade": "mason", "note": "", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9996771": { "jobs": {}, "name": "gabinomata-neri", "site_id": "d0-c14008-cw", "trade": "mason", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9999247": { "jobs": { "d0-c14008-cw": { "05-9032": { "g-05": { "hours_rwk": "false", "hours_st": "8l", "hours_ewo_id": "0", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "0l", "comments": "" } }, "05-9031": { "b-02": { "hours_rwk": "true", "hours_st": "0l", "hours_ewo_id": "0", "hours_ewo": "false", "hours_dt": "0l", "hours_ot": "1l", "comments": "" } } } }, "name": "franciscomontez", "site_id": "d0-c14008-cw", "trade": "mason", "note": "", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" }, "9999314": { "jobs": {}, "name": "romanhernandez", "site_id": "d0-c14008-cw", "trade": "mason", "note": "nowork", "signed_in": "false", "time_in": "06: 00", "time_out": "06: 00", "signed_out": "false" } } ]
.h file
#import <uikit/uikit.h> @interface tableviewcontroller : uitableviewcontroller @property (nonatomic, strong) nsarray *namearray; @property (nonatomic, strong) nsarray *dict; @property (nonatomic, strong) nsdata *data; @property (nonatomic, strong) nsstring *filepath; @end
.m file
#import "tableviewcontroller.h" #import "tableviewcell.h" @interface tableviewcontroller () @end @implementation tableviewcontroller - (void)viewdidload { [super viewdidload]; // uncomment following line preserve selection between presentations. // self.clearsselectiononviewwillappear = no; // uncomment following line display edit button in navigation bar view controller. // self.navigationitem.rightbarbuttonitem = self.editbuttonitem; nserror *error = nil; _filepath = [[nsbundle mainbundle] pathforresource:@"data" oftype:@"json"]; _data = [nsdata datawithcontentsoffile:_filepath]; _dict = [nsjsonserialization jsonobjectwithdata:_data options:0 error:&error]; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { //#warning incomplete implementation, return number of sections return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { //#warning incomplete implementation, return number of rows return _dict.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { tableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"tablecell" forindexpath:indexpath]; // configure cell... nsdictionary *dict = _dict[indexpath.row]; nsstring *name = dict[@"name"]; cell.titlelabel.text = name; return cell; }
your json misformatted. should be:
[ { "name": "xyz", "details": "xyz" }, { "name": "xyz", "details": "xyz" } ]
when executing _dict = [nsjsonserialization jsonobjectwithdata:_data options:0 error:&error];
array of dictionaries , not single dictionary. calling _namearray = [_dict objectforkey:@"name"];
should return nil
or crash.
here's implementation should like:
#import "tableviewcontroller.h" #import "tableviewcell.h" @interface tableviewcontroller () @end @implementation tableviewcontroller - (void)viewdidload { [super viewdidload]; nserror *error = nil; _filepath = [[nsbundle mainbundle] pathforresource:@"data" oftype:@"json"]; _data = [nsdata datawithcontentsoffile:_filepath]; // remember here, __dict array, kept same name code not dictionary. _dict = [nsjsonserialization jsonobjectwithdata:_data options:0 error:&error]; _namearray = [_dict objectforkey:@"name"]; } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return _dict.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { tableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"tablecell" forindexpath:indexpath]; // remember here, __dict array, kept same name code not dictionary. nsdictionary *dict = _dict[indexpath.row]; nsstring *name = dict[@"name"]; cell.titlelabel.text = name; return cell; }
Comments
Post a Comment