ios - Match clicked TableView row (string) to find corresponding unique ID -
i stuck on how best find , pass id of clicked table view row. i'd know best practice might accomplish this.
- in iphone storyboard want create
tableview
. - this tableview should populated json web server response.
- this json object contain around 10 items, name and
address plus unique id. - in tableview i'd have in each tableview row 1 name , address don't want have unique id displayed.
now here comes my question: if don't have unique id displayed how find id again based on row has been clicked? there way of assigning hidden fields? i'd send unique id via post web server.
the directions i've been investigating in far are...
- ...parse json object
nsdictionary
, match clicked tableview row id. - ...put json object sqllite database , match clicked row against database id.
- ...don't of above , send string of name , address (instead of id) web server matching against database take place.
what think best in terms of getting corresponding id clicked tableview row.
thanks lot time.
this how can accomplish this
call webservice , json. parse json , keep on adding object nsmutablearray itemlist.
create model class itemdesc have 3 fields name, address , id.
when parse json extract each individual product item model object defined above , add itemlist array.
it this:
nsarray *responsearray = responsefromservice; for(int i=0;i<responsearray.count;i++) { //parse object here nsdictionary *currentitem = [responsearray objectatindex:i]; itemdesc *itemobject = [[itemdesc alloc] init]; itemobject.name = [currentitem objectforkey:@"name"]; itemobject.address = [currentitem objectforkey:@"address"]; itemobject.id = [currentitem objectforkey:@"id"]; [itemlist addobject:itemobject]; itemobject=nil; }
now pass array object tableview row count , in cellforrowatindexpath row object below:
itemdesc *currentindexobj = [itemlist objectatindex:indexpath.row]; cell.name.text = currentindexobj.name; cell.address.text = currentindexobj.address;
this display name , address in tableviewcell.
to id when row select, in didselectrowatindexpath write below code:
itemdesc *currentindexobj = [itemlist objectatindex:indexpath.row]; nsstring *id = currentindexobj.id; nslog(@"id value: %@", id);
you have got id, perform whatever operation want perform.
model class nothing normal class itself. code create model class: go file -> new -> file ->ios-> source -> cocoatouch class
select next , given class name itemdesc , make subclass of nsobject. in project files itemdesc.h , itemdesc.m
itemdesc.h file:
#import <foundation/foundation.h> @interface itemdesc : nsobject @property(nonatomic,strong) nsstring *name; @property(nonatomic,strong) nsstring *address; @property(nonatomic,strong) nsstring *itemid;
@end
no need make changes in .m file. thats it. import header in class tableview exists , things work.
json structure:
[ { "name" : "john", "address" : "ny", "id" : "xyz" }, { "name" : "jane", "address" : "dc", "id" : "pqr" } ]
Comments
Post a Comment