UILocalizedIndexedCollation проблема: получение одинаковых результатов для каждого раздела

У меня есть массив:

 NSArray *nameArray = [NSArray arrayWithObjects:
                     //A
                     @"Alpha",@"Ar",@"Ax",
                     //B
                     @"B", ... nil];

В моем viewDidLoad я делаю следующее:

  _sectionedNameArray = [self partitionObjects:nameArray collationStringSelector:@selector(self)];

Вот метод partitionObjects:

-(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;
}

и следующее:

-(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];
}

Проблема:

Разделы не работают правильно. Я получаю "А" для раздела "А", затем случайным образом делю "А" для других разделов. Похоже, каждый раздел отсчитывает от начала массива... Есть идеи, что случилось с моим кодом?

Обновите следующий комментарий от 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;
}

0 ответов

Другие вопросы по тегам