ios - UITableview create alphabetic indexing from data array -
i have nsmutablearray populated sqlite query returns want display in uitableview in alphabetical order.
i know want display letter indexs see in contacts application etc.
however not sure how set indexs , create sections array each sections next letter in alphabet.
how go taking array , getting sections , indexs needed?
let's array (nsmutablearray myarray) letters looks this:
( { headertitle = v; rowvalues = ( { id = 60; name = "valley of giants (2)"; "real_name" = "valley of giants"; "wine_count" = " (2)"; } ); }, { headertitle = r; rowvalues = ( { id = 47; name = "rothbury estate (6)"; "real_name" = "rothbury estate"; "wine_count" = " (6)"; }, { id = 48; name = "rouge homme (4)"; "real_name" = "rouge homme"; }, { id = 45; name = "rawson\u2019s retreat (7)"; "real_name" = "rawson\u2019s retreat"; }, { id = 46; name = "rosemount estate (36)"; "real_name" = "rosemount estate"; } ); },
in table view, set number of sections
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { homecoming [myarray count]; }
number of rows in section:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section{ homecoming [[[myarray objectatindex:section] objectforkey:@"rowvalues"] count]; }
section title
-(nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section{ homecoming [[myarray objectatindex:section] valueforkey:@"headertitle"]; }
then within cellforrowatindexpath can access info this
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath{ nsstring *name= [[[[myarray objectatindex:indexpath.section] objectforkey:@"rowvalues"] objectatindex:indexpath.row] valueforkey:@"name"]; // , create cell do... }
if need sort array in alphabetical order can like:
nssortdescriptor *descriptor = [[nssortdescriptor alloc] initwithkey:@"headertitle" ascending:yes]; [myarray sortusingdescriptors:[nsarray arraywithobject:descriptor]];
ios objective-c arrays uitableview
No comments:
Post a Comment