Autogroup UITableView alphabetically

flo*_*hei 10 indexing objective-c alphabetized uitableview

Is there a way to auto group/auto-section my UITableView alphabetically? I have a huge array which is being displayed nicely, but it would be even better if I had the sections like in Contacts.app.

Best
–f

yas*_*urk 30

首先是定义部分

self.sections = [NSArray arrayWithObjects:@"#", @"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", @"l", @"m", @"n", @"o", @"p", @"q", @"r", @"s", @"t", @"u", @"v", @"w", @"x", @"y", @"z", nil];
Run Code Online (Sandbox Code Playgroud)

然后

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 27;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
   return self.sections;
}    

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
   return index;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   return [self.sections objectAtIndex:section];
}
Run Code Online (Sandbox Code Playgroud)

之后按字母表过滤

NSArray *sectionArray = [vendorList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [self.sections objectAtIndex:section]]]; 
            rowCount = [sectionArray count];
Run Code Online (Sandbox Code Playgroud)