Vja*_*del 7 objective-c uitableview ios sectionindexer
我有一个NSMutableArray:self.contact带有对象(名称按字母顺序排序):
(
"Anna Haro",
"Cheesy Cat",
"Daniel Higgins",
"David Taylor",
"Freckles Dog",
"Hank Zakroff",
"John Appleseed",
"Kate Be\U00e9ll"
)
Run Code Online (Sandbox Code Playgroud)
我用这行代码成功地在右边显示了字母:
- (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];
}
Run Code Online (Sandbox Code Playgroud)
现在,我要实现允许我访问好部分的方法?
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index {
}
Run Code Online (Sandbox Code Playgroud)
也许我要改变numberOfSections?这是我的代码:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1
}
Run Code Online (Sandbox Code Playgroud)
下一个 :
我做了两个阵列NSArray *test = [self.contact sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];:
(
"Anna Haro",
"Cheesy Cat",
"Daniel Higgins",
"David Taylor",
"Freckles Dog",
"Hank Zakroff",
"John Appleseed",
"Kate Be\U00e9ll"
)
Run Code Online (Sandbox Code Playgroud)
和
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);
Run Code Online (Sandbox Code Playgroud)
回报:
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"
);
}
Run Code Online (Sandbox Code Playgroud)
尝试以下链接。将帮助您轻松完成此操作。如果您在执行此操作时遇到任何问题,请告诉我。联系视图控制器部分标题和索引的教程
因此,对于非静态数据,假设您有一个名为 arrContacts 的联系人数组,那么您可以使用[arrContacts sortUsingSelector: @selector( localizedCaseInsensitiveCompare:)]now 轻松对其进行排序,以获取包含所有部分标题的数组,只需循环遍历该数组,并为每个对象将字符串修剪为第一个字母和如果尚不存在,则添加到该数组中。就是这样,现在您在一个数组中有一个部分列表,在另一个数组中有按字母顺序排序的联系人。让我知道事情的后续。;)
| 归档时间: |
|
| 查看次数: |
6102 次 |
| 最近记录: |