ios - How to populate data in a tableView as alphabetical order while using section index -
i'm implementing app need show tableview. tableview needs have alphabetical search on right side clicking on letter needs show data starting letter. i've implemented vertical alphabet search , when user clicks on letter takes user particular section. problem every section has same data it's not populating according alphabet.
here code this. // here exhibitorarray
contains info populate data in tableview.
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview { return [self.exhibitorarray count]; } - (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section { return [[[uilocalizedindexedcollation currentcollation] sectiontitles] objectatindex:section]; } - (nsarray *)sectionindextitlesfortableview:(uitableview *)tableview { return [[uilocalizedindexedcollation currentcollation] sectionindextitles]; } - (nsinteger)tableview:(uitableview *)tableview sectionforsectionindextitle:(nsstring *)title atindex:(nsinteger)index { return [[uilocalizedindexedcollation currentcollation] sectionforsectionindextitleatindex:index]; - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return [self.exhibitorarray count]; } - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:nil]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstylesubtitle reuseidentifier:cellidentifier]; } [self.exhibitortableview setseparatorinset:uiedgeinsetsmake(0, 0, 0, 0)]; uiview* bgview = [[uiview alloc] initwithframe:cgrectmake(0, 0, 1, 1)]; bgview.opaque = yes; bgview.backgroundcolor = [uicolor colorwithred:244.0f/255 green:245.0f/255 blue:246.0f/255 alpha:1.0]; [cell setbackgroundview:bgview]; uiimageview *defaultimage = [[uiimageview alloc]init]; [defaultimage setframe:cgrectmake(5.0f, 5.0f, 40.0f, 40.0f)]; [cell addsubview:defaultimage]; nsstring *urlstring = @"http://"; urlstring = [urlstring stringbyappendingstring:[nsstring stringwithformat:@"%@",[[self.exhibitorarray objectatindex:indexpath.row]valueforkey:@"image"]]]; nslog(@"%@",urlstring); asyncimageview *asyncimageview = [[asyncimageview alloc] initwithframe:cgrectmake(0.0f, 0.0f, 40.0f, 40.0f)]; [asyncimageview setbackgroundcolor:[uicolor clearcolor]]; [asyncimageview loadimagefromurl:[nsurl urlwithstring:urlstring]]; [defaultimage addsubview:asyncimageview]; defaultimage.contentmode = uiviewcontentmodescaleaspectfit; asyncimageview = nil; defaultimage = nil; nsstring *name_string = [[self.exhibitorarray objectatindex:indexpath.row]valueforkey:@"name"]; uilabel *user_name = [[ uilabel alloc]init ]; [user_name setframe:(cgrectmake(58, 5, 270, 25))]; [user_name setbackgroundcolor: [uicolor clearcolor]]; [user_name settext:name_string]; [user_name setfont:[uifont fontwithname:@"roboto-light" size:14]]; [user_name settextalignment:nstextalignmentleft]; [user_name settextcolor:[uicolor colorwithred:93.0f/255 green:94.0f/255 blue:94.0f/255 alpha:1.0]]; [cell addsubview:user_name]; user_name = nil; [cell setaccessorytype:uitableviewcellaccessorydisclosureindicator]; return cell; }
output of coming this.
the following wrong:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // return number of rows in section. return [self.exhibitorarray count]; }
you have separate arrays every section. let's different array letter 'a' , different array letter 'b' , on. then, based on section index, data correct array.
for example can use array of arrays array[section][index]
or dictionary dict['a'][index]
.
so above function has be:
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section { // based on array of arrays return [[self.exhibitorarray objectatindex:indexpath.section] count]; }
and then, in cellforrowatindexpath:
can use
nsstring *name_string = [[[self.exhibitorarray objectatindex:indexpath.section] objectatindex:indexpath.row] valueforkey:@"name"];
update: convert exhibitorarray
- (void)convertarray { //nsmutablearray *exhibitorarray = [[nsmutablearray alloc] initwitharray:@[@"abra", @"catabra"]]; nsarray *data = [exhibitorarray copy]; exhibitorarray = [[nsmutablearray alloc] initwithcapacity:27]; // 27 elements, 26 a-z plus 1 '#' (int i=0; i<27; i++) [exhibitorarray addobject:[[nsmutablearray alloc] init]]; int firstasciipos = (int)[@"a" characteratindex:0]; (int i=0; i<[data count]; i++) { int index = (int)[[[data objectatindex:i] uppercasestring] characteratindex:0] - firstasciipos; if (index < 0 || index > 25) { // not between , z, add '#' index = 26; } [[exhibitorarray objectatindex:index] addobject:[data objectatindex:i]]; } }
the above code converts array two-dimensional array based on alphabet letters. please feel free change according code , don't forget handle memory deallocations if not using arc.
Comments
Post a Comment