2013-02-07 17 views
0

我正在爲我的iphone應用程序創建聯繫人列表。我製作了一個包含以下屬性的自定義對象類。按照字母順序對對象排序並顯示按字母順序排列的表格標題,如Contact Book

  • 編號

現在我想打一個聯繫人列表像iPhone的聯繫人列表。所以用A-B-C-D-E -...作爲tableview節標題。我正在關注這個tutorial。 但他只是在使用字符串。我的問題在於CellForRow。在這裏你可以看到我有什麼ATM。

NSString *alphabet = [firstIndex objectAtIndex:[indexPath section]]; 

//---get all states beginning with the letter--- 
NSPredicate *predicate = 
[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", alphabet]; 
NSLog(@"list content is here %@",[listContent valueForKey:@"name"]); 
NSArray *contacts = [[listContent valueForKey:@"name"] filteredArrayUsingPredicate:predicate]; 
NSLog(@"Contacts array is %@",contacts); 

    Contact *contact = nil; 
    contact = [contacts objectAtIndex:[indexPath row]]; 
    NSLog(@"contact is in de else %@",contact.name); 
    NSString *text = [NSString stringWithFormat:@"%@ %@",contact.name,contact.firstName]; 
    cell.textLabel.text = text; 


    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 

我的第一行上崩潰,並顯示以下日誌

2013-02-07 10:52:47.963 Offitel2[7807:907] list content is here (
    Claes, 
    Geelen, 
    Verheyen 
) 
2013-02-07 10:52:47.964 Offitel2[7807:907] Contacts array is (
    Claes 
) 
2013-02-07 10:52:47.964 Offitel2[7807:907] -[__NSCFString name]: unrecognized selector sent to instance 0x208e2c20 
2013-02-07 10:52:47.965 Offitel2[7807:907] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString name]: unrecognized selector sent to instance 0x208e2c20' 

任何人可以幫助我嗎?

親切的問候

回答

3

遵循以下步驟:

  • 使文字的一切開始字母索引字母排列你要填充
  • 現在排序是:

    //sorting for english language 
    indexAlphabetArray = (NSMutableArray *)[indexAlphabetArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
    
  • 現在執行

    -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
    { 
        return indexAlphabetArray; 
    } 
    
  • 最後一個建議,採取一切名字的字典和組名與firstletter在字典的鍵,例如:

    nameDictionary:{ 
        A:(
         Astart, 
         Astop, 
        ) 
        b:(
         bstart, 
         bstop, 
        ) 
    } 
    
  • 這樣indexAlphabetArray = [nameDictionary allKeys];

編輯:允許讓它更容易:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[nameDictionary valueForKey:[indexAlphabetArray objectAtIndex:section]] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *kCellID = @"cellID"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID]; 
    } 
    NSArray *nameArray = [nameDictionary valueForKey:[indexAlphabetArray objectAtIndex:indexPath.section]]; 
    cell.textLabel.text = [nameArray objectAtIndex:indexPath.row]; 
    return cell;  
} 
+0

難道我不能只是自定義我的預測在某些方面?因爲一切工作除了在我的cellForRowAtIndexPath – Steaphann

+1

修復它!謝謝 – Steaphann

+0

您隨時歡迎。 :) – rptwsthi