uitableview - iOS tableview: Grouping rows into sections -
uitableview - iOS tableview: Grouping rows into sections -
i'm displaying dynamic list of names in table view, , i'm trying split them sections according first letter of name...
i've created array alphabetical list of letters
charindex = [[nsmutablearray alloc] init]; for(int i=0; i<[appdelegate.children count]-1; i++) { // person kid *achild = [appdelegate.children objectatindex:i]; // first letter of first name nsstring *firstletter = [achild.firstname substringtoindex:1]; nslog(@"first letter: %@", firstletter); // if index doesn't contain letter if(![charindex containsobject:firstletter]) { // add together index nslog(@"adding: %@", firstletter); [charindex addobject:firstletter]; } }
and i've set number of sections , title
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { // set number of sections in table match number of first letters homecoming [charindex count]; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { // set section title matching letter homecoming [charindex objectatindex:section]; }
but i'm having problem should in
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { }
you add together dictionary keeps track of number of people share same first letter. quick untested code:
charindex = [[nsmutablearray alloc] init]; charcount = [[nsmutabledictionary alloc] init]; for(int i=0; i<[appdelegate.children count]-1; i++) { // person kid *achild = [appdelegate.children objectatindex:i]; // first letter of first name nsstring *firstletter = [achild.firstname substringtoindex:1]; nslog(@"first letter: %@", firstletter); // if index doesn't contain letter if(![charindex containsobject:firstletter]) { // add together index nslog(@"adding: %@", firstletter); [charindex addobject:firstletter]; [charcount setobject:[nsnumber numberwithint:1] forkey:firstletter]; } [charcount setobject:[nsnumber numberwithint:[[charcount objectforkey:firstletter] intvalue] + 1] forkey:firstletter]; }
then in:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { [charcount objectforkey:[charindex objectatindex:section]]; }
ios uitableview
Comments
Post a Comment