2012-05-06 35 views
0

我想實現類似於iPhone的聯繫人列表。我知道有一個地址簿框架允許我訪問設備的聯繫人,但我想使用存儲在我的Core Data數據庫中的我自己的聯繫人。iOS實現「聯繫人視圖控制器」

我正在使用NSFetchedResultsController來獲取我的聯繫人並在列表視圖中列出。

但是,我似乎無法找到一種方法讓NSFetchedResultsController按部分顯示我的聯繫人,每個部分都是每個聯繫人姓名的第一個字母。像:

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

我認爲我可以在NSFetchedResultsControllerinit方法sectionNameKeyPath:參數做到這一點,但我怎麼用它來實現我的目的?

+0

我已經解決了我的問題,下面就這個問題的答案http://stackoverflow.com/questions/1112521/nsfetchedresultscontroller-with-sections-created-by-first-letter-一個字符串 –

回答

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; 
} 
+0

這不適合我。我希望章節的數量有所不同。例如,如果我沒有以「B」開頭的聯繫人,我不希望顯示一個「B」部分。我在其他評論中使用鏈接解決了我的問題。 –

相關問題