Как проиндексировать таблицу из массива объектов?

Доброе время суток! У меня есть массив и класс с свойствами, в который я мапаю данные, вот пример:

...
    Customers *customers;
    NSMutableArray *arrayCustomers;
...
        for (NSDictionary *dictionary in [responseData objectForKey:@"data_list"]) {
            customers = [[Customers alloc] init];
            
            for (NSString *key in [dictionary allKeys]) {
                if ([customers respondsToSelector:NSSelectorFromString(key)]) {
                    [customers setValue:[dictionary objectForKey:key] forKey:key];
                }
            }
            [arrayCustomers addObject:customers];
        }


+ Ко всему этому у меня есть поиск, выбиаю данные по нажатию на ячейку в таблице:
customers = [arrayCustomers objectAtIndex:[indexPath row]];


Суть задачи - проиндексировать таблицу, чтобы справа был ползунок и заголовок секций был буквой, подскажите, как это можно реализовать, когда такая история? :)
  • Вопрос задан
  • 2655 просмотров
Решения вопроса 1
morozovdenis
@morozovdenis
...
    Customers *customers;
    NSMutableDictionary *dictionaryCustomers;
    NSArray *sortedKeys = nil;
...
        for (NSDictionary *dictionary in [responseData objectForKey:@"data_list"]) {
            customers = [[Customers alloc] init];
            
            for (NSString *key in [dictionary allKeys]) {
                if ([customers respondsToSelector:NSSelectorFromString(key)]) {
                    [customers setValue:[dictionary objectForKey:key] forKey:key];
                }
            }
            //например по имени индексируем
            NSString *fisrtSymbol = [[customers.name substringFromIndex:1] uppercaseString];
            if ([dictionaryCustomers.allKeys containsObject:fisrtSymbol] == NO)
            {
                   dictionaryCustomers[fisrtSymbol] = [NSMutableArray new];
            }
            [dictionaryCustomers[fisrtSymbol]  addObject:customers];
        }
        sortedKeys = [dictionaryCustomers.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
            return [obj1 compare:obj2];
        }]


после этого
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return dictionaryCustomers.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [dictionaryCustomers[sortedKeys[section]] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Customers *c = [dictionaryCustomers[sortedKeys[indexPath.section]] obkectAtIndex:indexPath.row];

...

return cell;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return sortedKeys;
}


UPD:
фильтр:
@interface MyViewController ()
@property NSMutableDictionary *dictionaryCustomers; // исходные которые были заполненны из responseData
@property NSArray *sortedKeys; // исходные которые были заполненны из responseData

@property NSDictionary *currentDictionaryCustomers; 
@property NSArray *currentSortedKeys;

@end

@implemented MyViewController

- (void)parseResponse:(id)responseData
{
          // парсим ответ
          self.dictionaryCustomers = ...
          self.sortedKeys = ...

          self.currentDictionaryCustomers = self.dictionaryCustomers;
          self.currentSortedKeys = self.sortedKeys;
}

- (void)filterCustomers:(NSString *)sFilter
{
          NSMutableDictionary *result = [NSMutableDictionary new];
            
            for (NSString *key in [self.dictionaryCustomers allKeys])
            {
                for (Customers *customer in self.dictionaryCustomers[key])
                {
                        if (<customer.company содержит sFilter>)
                        {
                                  if ([result.allKeys containsObject:key] == NO) result[key] = [NSmutableArray new];
                                  
                                  [result[key] addObject:customer];
                        }
                }
            }
        self.currentDictionaryCustomers = result;
        self.currentSortedKeys = [result.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
            return [obj1 compare:obj2];
        }]

        [self.tableView reloadData];
}

// тут заполняем tableView из self.currentDictionaryCustomers и self.currentSortedKeys

@end
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы