objective c - Populate UITableView from JSON data -
i have json data
[{"id": 1,"name": "nodia","company": "www","position": "hr","email": "sss@www.com","number": "33","photo": "image1.png"}, {"id": 2,"name": "nona ","company": "sss","position": "head of department","email": "aaaa@samsda.ge","number": "5496","photo": "image2.png"}, {"id": 3,"name": "david","company": "ddd","position": "director","email": "test@sasd.e","number": "574","photo": "image3.png"},....]|
i create table view value name.
tableviewcontroller.h : @interface citiesviewcontroller () @property(nonatomic,strong)nsmutablearray *jsonarray @property(nonatomic,strong)nsmutablearray *citiesarray; -(void)retrievedata;
tableviewcontroller.m : @implementation citiesviewcontroller @synthesize citiesarray,jsonarray; #pragma mark - #pragma mark class methods -(void)retrievedata { nsurl *url = [nsurl urlwithstring:getdataurl]; nsdata *data = [nsdata datawithcontentsofurl:url]; jsonarray = [nsjsonserialization jsonobjectwithdata:data options:kniloptions error:nil]; citiesarray =[[nsmutablearray alloc]init]; (int i= 0; i<jsonarray.count; i++) { nsstring *cid = [[jsonarray objectatindex:i] objectforkey:@"id"]; nsstring *cname = [[jsonarray objectatindex:i] objectforkey:@"name"]; nsstring *ccompany = [[jsonarray objectatindex:i] objectforkey:@"company"]; nsstring *cposition = [[jsonarray objectatindex:i] objectforkey:@"position"]; nsstring *cemail = [[jsonarray objectatindex:i] objectforkey:@"email"]; nsstring *cnumber = [[jsonarray objectatindex:i] objectforkey:@"number"]; nsstring *cphoto = [[jsonarray objectatindex:i] objectforkey:@"photo"]; nsurl *urlone = [nsurl urlwithstring:cphoto]; // nslog(@"%@",urlone); nsdata *photodata = [nsdata datawithcontentsofurl:urlone]; uiimage *imageone = [[uiimage alloc] initwithdata:photodata]; [citiesarray addobject:[[city alloc]initwithcityname:cname andcitystate:ccompany andcitycountry:cemail andcitypopulation:cposition andcityid:cid andcitynumber:cnumber andcityphoto: (uiimage *) imageone]]; } [self.tableview reloaddata]; } - (void)viewdidload { [super viewdidload]; [self retrievedata]; } #pragma mark - table view data source - (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return 1; } - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { return citiesarray.count; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:@"cell" forindexpath:indexpath]; // configure cell... city *cityob; cityob=[citiesarray objectatindex:indexpath.row]; cell.textlabel.text=cityob.employeename; return cell; }
everything works fine.cell shows data. problem can not set title header section , section index titles in tableview.and want alphabetic header section because data dynamic static solution problem not suitable. like in tutriole
thanks much.forgive bad english!
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { return [citiesarray objectatindex:section]; }
please check out , make suitable modifications.
Comments
Post a Comment