A
A
Alexander Zarochintsev2014-03-30 23:13:42
Objective-C
Alexander Zarochintsev, 2014-03-30 23:13:42

How to index a table from an array of objects?

Good time of the day! I have an array and a class with properties, in which I map the data, here is an example:

...
    Customers *customers;
    NSMutableArray *arrayCustomers;
...
        for (NSDictionary *dictionary in [responseData objectForKey:@"data_list"]) {
            customers = ;

The essence of the task is to index the table so that there is a slider on the right and the title of the sections is a letter, tell me how this can be done when such a story? :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Morozov, 2014-03-31
@Kovalskiy

...
    Customers *customers;
    NSMutableDictionary *dictionaryCustomers;
    NSArray *sortedKeys = nil;
...
        for (NSDictionary *dictionary in [responseData objectForKey:@"data_list"]) {
            customers =  obkectAtIndex:indexPath.row];

...

return cell;
}

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

UPD:
filter:
@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

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question