iOS 实现了 "Contact View Controller"

标签 ios nsfetchedresultscontroller

我想实现类似于 iPhone 联系人列表的功能。我知道有一个地址簿框架允许我访问设备的联系人,但我想使用存储在核心数据数据库中的我自己的联系人。

我正在使用 NSFetchedResultsController 来获取并在表格 View 上列出我的联系人。

但是,我似乎找不到一种方法让 NSFetchedResultsController 显示按部分组织的联系人,每个部分都是每个联系人姓名的第一个字母。喜欢:

Section A: All contacts that start with the letter A
Section B: All contacts that start with the letter B
etc...

我认为我可以使用 NSFetchedResultsControllerinit 方法中的 sectionNameKeyPath: 参数来完成此操作,但是我应该如何使用为了达到我的目的?

最佳答案

您必须首先对数据进行排序,并获取每个部分的“键”(A、B、C、D、E)列表。将它们保存为属性上的 NSArray。

实现这个:

// Function to load your data
-(void)dataDidLoad
{
// sort the keys
        self.sortedKeysForUsersWithApp = tSortedKeys;
//
        [self.tableView reloadData];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// add a UILabel
    headerLabel.text = [self.sortedKeysForUsersWithApp objectAtIndex:section];
// set the text to the section title
}

-(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];
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    //return [self.sortedKeys indexOfObject:title];
    return [self.sortedKeysForUsersWithApp indexOfObject:title];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //return self.sortedKeys.count;
    return self.sortedKeysForUsersWithApp.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *tDictionary = self.sortedUsersWithApp;

    //NSString *key = [self.sortedKeys objectAtIndex:section];
    NSString *key = [self.sortedKeysForUsersWithApp objectAtIndex:section];
    return ((NSArray*)[tDictionary objectForKey:key]).count;
}

关于iOS 实现了 "Contact View Controller",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10472116/

相关文章:

ios - 为什么不调用连接 didFailWithError ?

ios - 将 currentPlaybackTime 与 MPMoviePlayerController 一起用于 mp3 文件

c++ - 无法使用 Xcode 9 运行项目,许多新的 C++ 语义问题

objective-c - NSFetchedResultsController 与搜索

iphone - 使用 NSFetchedResultsController 进行分页?

swift - nsfetchedresultscontroller 在 swift 中删除具有名称的缓存

ios - 重置上下文不会触发 fetchResultedController

ios - PhoneGap iOS 提交错误 : ITMS-90807: Invalid entitlement - How can I build with SDK 13 or later?

iOS Swift 3 图像向左旋转 90 度

iphone - 非法尝试在不同上下文中的对象之间建立关系 'xyz'