2015-08-01 42 views
5

Tôi có một NSMutableArray: self.contact với các đối tượng (tên được sắp xếp theo thứ tự abc):Sắp xếp tiếp xúc theo thứ tự abc Mục UITableView

(
    "Anna Haro", 
    "Cheesy Cat", 
    "Daniel Higgins", 
    "David Taylor", 
    "Freckles Dog", 
    "Hank Zakroff", 
    "John Appleseed", 
    "Kate Be\U00e9ll" 
) 

Tôi thành công để hiển thị ở bên phải bảng chữ cái với dòng mã này:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { 
     return[NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I", @"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z", nil]; 
    } 

Bây giờ, tôi đã triển khai phương pháp cho phép tôi truy cập vào phần tốt?

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index { 

} 

Và có lẽ tôi đã thay đổi numberOfSections? Đây là mã của tôi:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
return 1 
} 

tiếp theo:

Tôi đã thực hiện hai mảng: NSArray *test = [self.contact sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];:

(
    "Anna Haro", 
    "Cheesy Cat", 
    "Daniel Higgins", 
    "David Taylor", 
    "Freckles Dog", 
    "Hank Zakroff", 
    "John Appleseed", 
    "Kate Be\U00e9ll" 
) 

NSMutableDictionary dicoAlphabet:

// Dictionary will hold our sub-arrays 
    self.dicoAlphabet = [NSMutableDictionary dictionary]; 

    // Iterate over all the values in our sorted array 
    for (NSString *value in test) { 

     // Get the first letter and its associated array from the dictionary. 
     // If the dictionary does not exist create one and associate it with the letter. 
     NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)]; 
     NSMutableArray *arrayForLetter = [self.dicoAlphabet objectForKey:firstLetter]; 
     if (arrayForLetter == nil) { 
      arrayForLetter = [NSMutableArray array]; 
      [self.dicoAlphabet setObject:arrayForLetter forKey:firstLetter]; 
     } 

     // Add the value to the array for this letter 
     [arrayForLetter addObject:value]; 
    } 

    // arraysByLetter will contain the result you expect 
    NSLog(@"Dictionary: %@", self.dicoAlphabet); 

trả về:

Dictionary: { 
    A =  (
     "Anna Haro" 
    ); 
    C =  (
     "Cheesy Cat" 
    ); 
    D =  (
     "Daniel Higgins", 
     "David Taylor" 
    ); 
    F =  (
     "Freckles Dog" 
    ); 
    H =  (
     "Hank Zakroff" 
    ); 
    J =  (
     "John Appleseed" 
    ); 
    K =  (
     "Kate Be\U00e9ll" 
    ); 
} 

Trả lời

1

thử liên kết sau. Sẽ giúp bạn làm điều này khá dễ dàng. Hãy cho tôi biết nếu bạn có bất kỳ vấn đề nào trong khi bạn làm điều này. Vì vậy, đối với dữ liệu không tĩnh, chúng ta hãy nói rằng bạn có một loạt các địa chỉ liên lạc được gọi là arrContacts sau đó bạn có thể dễ dàng sắp xếp nó bằng cách sử dụng [arrContacts sortUsingSelector: @selector(localizedCaseInsensitiveCompare:)] ngay bây giờ để có được một mảng với tất cả các tiêu đề phần chỉ vòng qua mảng và cho mỗi đối tượng trim chuỗi ký tự đầu tiên và thêm vào mảng này nếu chưa có. Thats nó, bây giờ bạn có một danh sách phần trong một mảng và sắp xếp theo thứ tự bảng chữ cái trong khác. Cho tôi biết làm thế nào nó đi. ;)

+0

hey người đàn ông, cảm ơn bạn đã trả lời! Tôi đã nhìn thấy tuto này, đó là một tuto tốt, nhưng nó cho dữ liệu tĩnh (động vật), tôi muốn thay thế động vật = @ {@ "B": @ [@ "Gấu", @ "Black Swan", @ "Buffalo"] ... với dữ liệu biến (danh sách liên lạc) ... Bạn có biết làm thế nào để làm điều này? – Viny76

+0

Chỉ cần chỉnh sửa câu trả lời của tôi, hãy cho tôi biết nếu điều đó có ích. – Aish

+0

Cảm ơn người đàn ông! Tôi đã cập nhật câu hỏi của mình :) Tôi sẽ làm gì tiếp theo? – Viny76

Các vấn đề liên quan