iOS: алфавитные разделы для просмотра таблицы из API - PullRequest
1 голос
/ 19 января 2012

Я получаю список имен клиентов из API. У меня есть около 11 000 имен, и я отображаю их в виде таблицы с возможностью поиска. Я хотел бы создать разделы, как в адресной книге, где есть буквы алфавита и индекс справа. Все учебники, которые я видел, упоминали использование fetchRequests, но так как я получаю информацию из API, я не думаю, что это сработает.

Может кто-нибудь помочь мне сделать так, чтобы я мог отобразить список с заголовками разделов и индексом справа? У меня уже настроено отображение только имен, и он доступен для поиска.

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
        //self.customers = [json objectForKey:@"data"];
        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            //NSLog(@"Dict: %@, %@",[dict objectForKey:@"last_name"], [dict objectForKey:@"first_name"]);
            [tempCustomers addObject:[NSString stringWithFormat:@"%@, %@",[dict objectForKey:@"last_name"],[dict objectForKey:@"first_name"]]];
            //NSLog(@"Size: %i - %@", tempCustomers.count, [tempCustomers lastObject]);
        }
        self.customers = tempCustomers;
        //NSLog(@"Received: %@",tempCustomers);
        NSLog(@"Customers Count: %i", self.customers.count);
        tempCustomers = nil;

EDIT: Это лучший способ продолжить, если у меня будут тысячи имен?

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSMutableArray *tempCustomers = [[NSMutableArray alloc] init];
        NSString *name;
        customerSections = [[NSMutableArray alloc] init];
        //self.customers = [json objectForKey:@"data"];
        for (NSDictionary *dict in [json objectForKey:@"data"]) {
            name = [NSString stringWithFormat:@"%@, %@",[dict objectForKey:@"last_name"],[dict objectForKey:@"first_name"]];
            //NSLog(@"Dict: %@, %@",[dict objectForKey:@"last_name"], [dict objectForKey:@"first_name"]);
            [tempCustomers addObject:name];
            if (![customerSections containsObject:[[name substringToIndex:1] uppercaseString]]) {
                [customerSections addObject:[[name substringToIndex:1] uppercaseString]];
            }
            //NSLog(@"Size: %i - %@", tempCustomers.count, [tempCustomers lastObject]);
        }
        NSLog(@"Full customer Sections %@",customerSections);
        self.customers = tempCustomers;
        //NSLog(@"Received: %@",tempCustomers);
        NSLog(@"Customers Count: %i", self.customers.count);
        tempCustomers = nil;

ОБНОВЛЕНИЕ 2: Я изменил массив на словарь, где ключ - это буква, а значение - индекс, с которого начинается ключ. Прямо сейчас это показывает странные числа. Смотрите журнал ниже.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [customerSections count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Section: %i", section);
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return self.filteredCustomers.count;
    } else {
        NSLog(@"Section before conversion %@, %@",[customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section]],[[customerSections allKeys] objectAtIndex:section]);
        NSInteger current = (NSInteger)[customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section]];
        //NSString *next = [customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section + 1]];
        if ((NSInteger)[customerSections objectForKey:[[customerSections allKeys] lastObject]] != current) {
            NSLog(@"Returning count: %i which was %@ - %@",(NSInteger)[customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section + 1]] - current,[customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section + 1]] ,current);
            return (NSInteger)[customerSections objectForKey:[[customerSections allKeys] objectAtIndex:section + 1]] - current;
        } else {
            NSLog(@"Returning last count %i of %i - %i",self.customers.count - current, self.customers.count, current);
            return self.customers.count - current;
        }
        return self.customers.count;
    }
}

LOG 2012-01-19 10: 42: 38.751 MyApp [9900: 11503] Полный раздел клиента { "" = 22; "(" = 30; "," = 0; "" = 31; 1 = 42; 2 = 43; "?" = 44; А = 46; B = 407; С = 1156; D = 1820; Е = 2304; F = 2508; G = 2833; H = 3290; I = 3869; J = 3898; К = 4166; L = 4546; М = 5047; N = 5888; O = 6070; P = 6224; Q = 6706; R = 6725; S = 7200; Т = 8114; U = 8461; V = 8498; W = 8666; Х = 9129; Y = 9134; Z = 9197; }

2012-01-19 10:42:38.752 MyApp[9900:11503] Customers Count: 9258
2012-01-19 10:42:38.753 MyApp[9900:f803] Title for Section: 32 - J
2012-01-19 10:42:38.753 MyApp[9900:f803] Section: 32
2012-01-19 10:42:38.754 MyApp[9900:f803] Section before conversion 3898, J
2012-01-19 10:42:38.754 MyApp[9900:f803] Returning last count -269873302 of 9258 - 269882560
2012-01-19 10:42:38.755 MyApp[9900:f803] Title for Section: 0 - K
2012-01-19 10:42:38.755 MyApp[9900:f803] Section: 0
2012-01-19 10:42:38.756 MyApp[9900:f803] Section before conversion 4166, K
2012-01-19 10:42:38.756 MyApp[9900:f803] Returning count: 42608 which was 4546 - 4166
2012-01-19 10:42:38.757 MyApp[9900:f803] Title for Section: 1 - L
2012-01-19 10:42:38.758 MyApp[9900:f803] Section: 1
2012-01-19 10:42:38.758 MyApp[9900:f803] Section before conversion 4546, L
2012-01-19 10:42:38.759 MyApp[9900:f803] Returning count: 56432 which was 5047 - 4546
2012-01-19 10:42:38.759 MyApp[9900:f803] Title for Section: 2 - M
2012-01-19 10:42:38.760 MyApp[9900:f803] Section: 2
2012-01-19 10:42:38.760 MyApp[9900:f803] Section before conversion 5047, M
2012-01-19 10:42:38.761 MyApp[9900:f803] Returning count: 94528 which was 5888 - 5047
2012-01-19 10:42:38.762 MyApp[9900:f803] Title for Section: 3 - N
2012-01-19 10:42:38.763 MyApp[9900:f803] Section: 3
2012-01-19 10:42:38.763 MyApp[9900:f803] Section before conversion 5888, N
2012-01-19 10:42:38.768 MyApp[9900:f803] Returning count: 20368 which was 6070 - 5888
2012-01-19 10:42:38.769 MyApp[9900:f803] Title for Section: 4 - O
2012-01-19 10:42:38.769 MyApp[9900:f803] Section: 4
2012-01-19 10:42:38.770 MyApp[9900:f803] Section before conversion 6070, O
2012-01-19 10:42:38.771 MyApp[9900:f803] Returning count: -695200 which was 22 - 6070
2012-01-19 10:42:38.771 MyApp[9900:f803] Title for Section: 5 -  
2012-01-19 10:42:38.771 MyApp[9900:f803] Section: 5
2012-01-19 10:42:38.772 MyApp[9900:f803] Section before conversion 22,  
2012-01-19 10:42:38.772 MyApp[9900:f803] Returning count: 712496 which was 6224 - 22
2012-01-19 10:42:38.780 MyApp[9900:f803] Title for Section: 6 - P
2012-01-19 10:42:38.781 MyApp[9900:f803] Section: 6
2012-01-19 10:42:38.781 MyApp[9900:f803] Section before conversion 6224, P
2012-01-19 10:42:38.782 MyApp[9900:f803] Returning count: 54080 which was 6706 - 6224
2012-01-19 10:42:38.782 MyApp[9900:f803] Title for Section: 7 - Q
2012-01-19 10:42:38.783 MyApp[9900:f803] Section: 7
2012-01-19 10:42:38.783 MyApp[9900:f803] Section before conversion 6706, Q
2012-01-19 10:42:38.784 MyApp[9900:f803] Returning count: 2128 which was 6725 - 6706
2012-01-19 10:42:38.784 MyApp[9900:f803] Title for Section: 8 - R
2012-01-19 10:42:38.785 MyApp[9900:f803] Section: 8
2012-01-19 10:42:38.785 MyApp[9900:f803] Section before conversion 6725, R
2012-01-19 10:42:38.786 MyApp[9900:f803] Returning count: 53440 which was 7200 - 6725
2012-01-19 10:42:38.786 MyApp[9900:f803] Title for Section: 9 - S
2012-01-19 10:42:38.787 MyApp[9900:f803] Section: 9
2012-01-19 10:42:38.787 MyApp[9900:f803] Section before conversion 7200, S
2012-01-19 10:42:38.788 MyApp[9900:f803] Returning count: 102576 which was 8114 - 7200
2012-01-19 10:42:38.789 MyApp[9900:f803] Title for Section: 10 - T
2012-01-19 10:42:38.790 MyApp[9900:f803] Section: 10
2012-01-19 10:42:38.790 MyApp[9900:f803] Section before conversion 8114, T
2012-01-19 10:42:38.791 MyApp[9900:f803] Returning count: 38992 which was 8461 - 8114
2012-01-19 10:42:38.791 MyApp[9900:f803] Title for Section: 11 - U
2012-01-19 10:42:38.792 MyApp[9900:f803] Section: 11
2012-01-19 10:42:38.792 MyApp[9900:f803] Section before conversion 8461, U
2012-01-19 10:42:38.793 MyApp[9900:f803] Returning count: 4192 which was 8498 - 8461
2012-01-19 10:42:38.793 MyApp[9900:f803] Title for Section: 12 - V
2012-01-19 10:42:38.794 MyApp[9900:f803] Section: 12
2012-01-19 10:42:38.794 MyApp[9900:f803] Section before conversion 8498, V
2012-01-19 10:42:38.794 MyApp[9900:f803] Returning count: -965296 which was 44 - 8498
2012-01-19 10:42:38.795 MyApp[9900:f803] Title for Section: 13 - ?
2012-01-19 10:42:38.795 MyApp[9900:f803] Section: 13
2012-01-19 10:42:38.796 MyApp[9900:f803] Section before conversion 44, ?
2012-01-19 10:42:38.796 MyApp[9900:f803] Returning count: 984192 which was 8666 - 44
2012-01-19 10:42:38.804 MyApp[9900:f803] Title for Section: 14 - W
2012-01-19 10:42:38.805 MyApp[9900:f803] Section: 14
2012-01-19 10:42:38.805 MyApp[9900:f803] Section before conversion 8666, W
2012-01-19 10:42:38.805 MyApp[9900:f803] Returning count: -985776 which was 30 - 8666
2012-01-19 10:42:38.806 MyApp[9900:f803] Title for Section: 15 - (
2012-01-19 10:42:38.806 MyApp[9900:f803] Section: 15
2012-01-19 10:42:38.807 MyApp[9900:f803] Section before conversion 30, (
2012-01-19 10:42:38.807 MyApp[9900:f803] Returning count: 1037712 which was 9129 - 30
2012-01-19 10:42:38.815 MyApp[9900:f803] Title for Section: 16 - X
2012-01-19 10:42:38.816 MyApp[9900:f803] Section: 16
2012-01-19 10:42:38.816 MyApp[9900:f803] Section before conversion 9129, X
2012-01-19 10:42:38.817 MyApp[9900:f803] Returning count: -1035568 which was 46 - 9129
2012-01-19 10:42:38.817 MyApp[9900:f803] Title for Section: 17 - A
2012-01-19 10:42:38.817 MyApp[9900:f803] Section: 17
2012-01-19 10:42:38.833 MyApp[9900:f803] Section before conversion 46, A
2012-01-19 10:42:38.834 MyApp[9900:f803] Returning count: 1036048 which was 9134 - 46
2012-01-19 10:42:38.842 MyApp[9900:f803] Title for Section: 18 - Y
2012-01-19 10:42:38.843 MyApp[9900:f803] Section: 18
2012-01-19 10:42:38.843 MyApp[9900:f803] Section before conversion 9134, Y
2012-01-19 10:42:38.843 MyApp[9900:f803] Returning count: -1799248 which was 407 - 9134
2012-01-19 10:42:38.844 MyApp[9900:f803] Title for Section: 19 - B
2012-01-19 10:42:38.844 MyApp[9900:f803] Section: 19
2012-01-19 10:42:38.845 MyApp[9900:f803] Section before conversion 407, B
2012-01-19 10:42:38.845 MyApp[9900:f803] Returning count: 1806464 which was 9197 - 407
2012-01-19 10:42:38.858 MyApp[9900:f803] Title for Section: 20 - Z
2012-01-19 10:42:38.859 MyApp[9900:f803] Section: 20
2012-01-19 10:42:38.863 MyApp[9900:f803] Section before conversion 9197, Z
2012-01-19 10:42:38.864 MyApp[9900:f803] Returning count: -1890576 which was 1156 - 9197
2012-01-19 10:42:38.864 MyApp[9900:f803] Title for Section: 21 - C
2012-01-19 10:42:38.865 MyApp[9900:f803] Section: 21
2012-01-19 10:42:38.866 MyApp[9900:f803] Section before conversion 1156, C
2012-01-19 10:42:38.866 MyApp[9900:f803] Returning count: 842016 which was 0 - 1156
2012-01-19 10:42:38.873 MyApp[9900:f803] Title for Section: 22 - ,
2012-01-19 10:42:38.874 MyApp[9900:f803] Section: 22
2012-01-19 10:42:38.875 MyApp[9900:f803] Section before conversion 0, ,
2012-01-19 10:42:38.875 MyApp[9900:f803] Returning count: -916544 which was 1820 - 0
2012-01-19 10:42:38.876 MyApp[9900:f803] Title for Section: 23 - D
2012-01-19 10:42:38.876 MyApp[9900:f803] Section: 23
2012-01-19 10:42:38.876 MyApp[9900:f803] Section before conversion 1820, D
2012-01-19 10:42:38.877 MyApp[9900:f803] Returning count: -54048 which was 2304 - 1820
2012-01-19 10:42:38.877 MyApp[9900:f803] Title for Section: 24 - E
2012-01-19 10:42:38.878 MyApp[9900:f803] Section: 24
2012-01-19 10:42:38.878 MyApp[9900:f803] Section before conversion 2304, E
2012-01-19 10:42:38.879 MyApp[9900:f803] Returning count: 973920 which was 31 - 2304
2012-01-19 10:42:38.886 MyApp[9900:f803] Title for Section: 25 - .
2012-01-19 10:42:38.887 MyApp[9900:f803] Section: 25
2012-01-19 10:42:38.887 MyApp[9900:f803] Section before conversion 31, .
2012-01-19 10:42:38.887 MyApp[9900:f803] Returning count: 294144 which was 2508 - 31
2012-01-19 10:42:38.890 MyApp[9900:f803] Title for Section: 26 - F
2012-01-19 10:42:38.891 MyApp[9900:f803] Section: 26
2012-01-19 10:42:38.891 MyApp[9900:f803] Section before conversion 2508, F
2012-01-19 10:42:38.892 MyApp[9900:f803] Returning count: 36432 which was 2833 - 2508
2012-01-19 10:42:38.892 MyApp[9900:f803] Title for Section: 27 - G
2012-01-19 10:42:38.893 MyApp[9900:f803] Section: 27
2012-01-19 10:42:38.893 MyApp[9900:f803] Section before conversion 2833, G
2012-01-19 10:42:38.893 MyApp[9900:f803] Returning count: 51264 which was 3290 - 2833
2012-01-19 10:42:38.894 MyApp[9900:f803] Title for Section: 28 - H
2012-01-19 10:42:38.895 MyApp[9900:f803] Section: 28
2012-01-19 10:42:38.895 MyApp[9900:f803] Section before conversion 3290, H
2012-01-19 10:42:38.896 MyApp[9900:f803] Returning count: -380576 which was 42 - 3290
2012-01-19 10:42:38.896 MyApp[9900:f803] Title for Section: 29 - 1
2012-01-19 10:42:38.897 MyApp[9900:f803] Section: 29
2012-01-19 10:42:38.897 MyApp[9900:f803] Section before conversion 42, 1
2012-01-19 10:42:38.897 MyApp[9900:f803] Returning count: 445376 which was 3869 - 42
2012-01-19 10:42:38.901 MyApp[9900:f803] Title for Section: 30 - I
2012-01-19 10:42:38.902 MyApp[9900:f803] Section: 30
2012-01-19 10:42:38.902 MyApp[9900:f803] Section before conversion 3869, I
2012-01-19 10:42:38.903 MyApp[9900:f803] Returning count: -445312 which was 43 - 3869
2012-01-19 10:42:38.903 MyApp[9900:f803] Title for Section: 31 - 2
2012-01-19 10:42:38.904 MyApp[9900:f803] Section: 31
2012-01-19 10:42:38.904 MyApp[9900:f803] Section before conversion 43, 2
2012-01-19 10:42:38.905 MyApp[9900:f803] Returning count: 448816 which was 3898 - 43

ЗАКЛЮЧИТЕЛЬНОЕ РЕДАКТИРОВАНИЕ: Я пошел с UILocalizedCollation и этот учебник http://benedictcohen.co.uk/blog/archives/230

1 Ответ

2 голосов
/ 19 января 2012

Если бы вы использовали CoreData, вы бы использовали fetchRequests и, возможно, fetchedResultsController .... но вы не используете CoreData, так что забудьте об этом ...

Что вы хотите посмотретьat UITableView методы источника данных sectionIndexTitlesForTableView: и tableView:sectionForSectionIndexTitle:atIndex:.Первый метод - это когда вы возвращаете заголовки вашего индекса, возможно, NSArray, содержащий строки @ "A", @ "B" и т. Д. Второй метод помогает табличному представлению узнать, где найти данные для раздела из ваших данных.источник.Вы должны прочитать об этих методах, поскольку они имеют решающее значение для того, что вы пытаетесь достичь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...