简体   繁体   中英

Sorting a tableview alphabetically with objects and showing alphabetic tableview headers, as Contact Book

I am working on a contact list for my iphone applications. I have made a custom object class that contains the following attributes.

  • Firstname
  • Lastname
  • Id

Now I want to make a contact list like the iphone's contact list. So with ABCDE-... as the tableview section headers. I was following this tutorial . But he is just working with strings. My problem is lying inside the CellForRow. Here you can see what I have 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];

I crashes on the first row with the following Log

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'

Can anybody help me with this?

Kind regards

Follow these steps:

  • make an index alphabet array with all starting alphabet of words you are going to populate
  • Now sort it:

     //sorting for english language indexAlphabetArray = (NSMutableArray *)[indexAlphabetArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
  • Now implement

     -(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return indexAlphabetArray; } 
  • Final suggestion, take a dictionary of all names and group name with firstletter as key in dictionary, Eg.:

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

Edit: Lets make it more easy for you:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM