【问题标题】:iOS implementing a "Contact View Controller"iOS 实现“联系人视图控制器”
【发布时间】:2012-05-15 08:56:39
【问题描述】:

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

我正在使用NSFetchedResultsController在表格视图中获取和列出我的联系人。

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

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

我认为我可以使用init 方法中的sectionNameKeyPath: 参数来实现NSFetchedResultsController,但我应该如何使用它来达到我的目的?

【问题讨论】:

标签: ios nsfetchedresultscontroller


【解决方案1】:

您必须先对数据进行排序,然后获取每个部分的“键”(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;
}

【讨论】:

  • 这对我不起作用。我希望部分的数量有所不同。例如,如果我没有以“B”开头的联系人,我不希望出现“B”部分。我已经使用其他评论中的链接解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多