...
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]];
...
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;
}
@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