Reputation: 863
I have an array:
NSArray *nameArray = [NSArray arrayWithObjects:
//A
@"Alpha",@"Ar",@"Ax",
//B
@"B", ... nil];
In my viewDidLoad I do the following:
_sectionedNameArray = [self partitionObjects:nameArray collationStringSelector:@selector(self)];
Here's the partitionObjects method:
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
_collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[_collation sectionTitles] count]; //section count is take from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
//create an array to hold the data for each section
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}
//put each object into a section
for (id object in array)
{
NSInteger index = [_collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
//sort each section
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[_collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
and the following:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[_sectionedNameArray objectAtIndex:section] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[_collation sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [_collation sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
The sections are not working correctly. I get "A" for section "A" then randomly divided "A"s for the other sections. It looks like each section is counting back from the beginning of the array... Any ideas what's wrong with my code?
Update following comment from rmaddy:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"hCell";
NameCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.hNameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.hCityLabel.text = [cityArray objectAtIndex:indexPath.row];
cell.hPoneLabel.text = [phoneArray objectAtIndex:indexPath.row];
return cell;
}
Upvotes: 1
Views: 509