ios - Sectioned table view indexpath issue -


i have table view controller states listed in alphabetical sections. clicking on state returns detailed information state via web service call. first attempt @ sectioned or grouped table view , having trouble index path on rows past 'a'.

for instance, if click on 'california' first item in 'c' group, indexpath.row property 0 instead of 4, which, since ca fifth state in alphabetical order, should have indexpath.row of 4.

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {     static nsstring *cellidentifier = @"cell";     uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];      //get letter in current section     nsstring *letter = [stateindex objectatindex:[indexpath section]];     //get states beginning letter     nspredicate *predicate = [nspredicate predicatewithformat:@"self beginswith[c] %@", letter];     nsarray *states = [statekeys filteredarrayusingpredicate:predicate];     if([states count] > 0){         //get relevant state states object         nsstring *cellvalue = [states objectatindex:indexpath.row];         [[cell textlabel] settext:cellvalue];             }         return cell; } 

and in seque, setting break point on last line in method reveals itemrowindex incorrect (except, of course, 'a' group):

-(void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender{     if([segue.identifier isequaltostring:@"sgshowstaterivers"]){         riversbystatetableviewcontroller *riversvc = [segue destinationviewcontroller];         nsindexpath *indexpath = [self.tableview indexpathforselectedrow];         nsinteger itemrowindex = indexpath.row;         nsstring *key = [statekeys objectatindex:itemrowindex];         [riversvc setstateidentifier:[statesdict objectforkey:key]];     } } 

how 1 have grouped table view items still numbered in original array? thanks! v

your understanding of nsindexpaths in context of uitableview incorrect. row property resets 0 each individual section in table.

you have 2 easy options move forward:

  • what can make 2-dimensional array (an "array of arrays") first level each section , second level each row in section. lets directly use row , section of index path make array lookup.

  • in prepareforsegue:sender: can loop on sections , count rows until indexpathforselectedrow.section , add indexpathforselectedrow.row count. give index can use fetch state info 1-dimensional array.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -