iOS:来自API的tableview的字母部分

时间:2012-01-18 21:16:12

标签: iphone objective-c ios5 uitableview

我从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;

编辑: 如果我要拥有数千个名字,这是最好的方法吗?

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;     A = 46;     B = 407;     C = 1156;     D = 1820;     E = 2304;     F = 2508;     G = 2833;     H = 3290;     我= 3869;     J = 3898;     K = 4166;     L = 4546;     M = 5047;     N = 5888;     O = 6070;     P = 6224;     Q = 6706;     R = 6725;     S = 7200;     T = 8114;     U = 8461;     V = 8498;     W = 8666;     X = 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 个答案:

答案 0 :(得分:2)

如果您使用的是CoreData,那么您将使用fetchRequests,也许还会使用fetchedResultsController ......但是您没有使用CoreData,所以不要忘记那些......

您要查看的是UITableView数据源方法sectionIndexTitlesForTableView:tableView:sectionForSectionIndexTitle:atIndex:。第一种方法是返回索引标题,可能是包含字符串@“A”,@“B”等的NSArray。第二种方法可以帮助表格视图知道从数据中找到该节的数据的位置资源。您应该阅读这些方法,因为它们对您要完成的任务至关重要。