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"
)
và
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"
);
}
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
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
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