Reputation: 381
I'm creating a UITableView
with a list of the users contacts. I've successfully imported the list of contacts and am storing them within an array as a dictionary containing each contact's forename, last name & ID reference. I'm trying to implement the UITableView's
indexed list function.
I'm hitting problems when trying to partition the array into the different sections in the table's index UILocalizedIndexedCollation
. Specifically by the selector. I want to be able to choose whether to sort the names by the forename or surname.
- (void)getContacts
{
// Do any additional setup after loading the view, typically from a nib.
_addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(_addressBook, ^(bool granted, CFErrorRef error) {
NSLog(@"GRANTED");
});
NSArray *rawContactData = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBook);
NSMutableArray *newContactList = [[NSMutableArray alloc] init];
for (int i = 0; i < [rawContactData count]; i++) {
ABRecordRef contact = (__bridge ABRecordRef)rawContactData[i];
NSDictionary *contactData = @{ @"name" : (__bridge_transfer NSString *)ABRecordCopyValue(contact, kABPersonFirstNameProperty),
@"surname" : (__bridge_transfer NSString *)ABRecordCopyValue(contact, kABPersonLastNameProperty),
@"recordID" : [NSNumber numberWithInt:ABRecordGetRecordID(contact)]};
[newContactList addObject:contactData];
}
_tableData = [NSMutableArray arrayWithArray:[self partitionObjects:newContactList collationStringSelector:@selector(name)]];
}
-(NSArray *)partitionObjects:(NSArray *)array collationStringSelector:(SEL)selector
{
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
NSInteger sectionCount = [[collation sectionTitles] count]; //section count is take from sectionTitles and not sectionIndexTitles
NSMutableArray *unsortedSections = [NSMutableArray arrayWithCapacity:sectionCount];
//create an array to hold the data for each section
for(int i = 0; i < sectionCount; i++)
{
[unsortedSections addObject:[NSMutableArray array]];
}
//put each object into a section
for (NSMutableDictionary *object in array) {
NSInteger index = [collation sectionForObject:object collationStringSelector:selector];
[[unsortedSections objectAtIndex:index] addObject:object];
}
NSMutableArray *sections = [NSMutableArray arrayWithCapacity:sectionCount];
//sort each section
for (NSMutableArray *section in unsortedSections)
{
[sections addObject:[collation sortedArrayFromArray:section collationStringSelector:selector]];
}
return sections;
}
Upvotes: 1
Views: 649
Reputation: 1064
Selector is sent to the Object which you are sorting in array. In your case your object is type Dictionary which does not implement selector "name" or surname etc. You should create a simple class instead of using dictionary. and just declare properties to your liking
@interface Contact : NSObject
@property (strong,nonatomic) NSString *fname;
@property (strong,nonatomic) NSString *lname;
@property (strong,nonatomic) NSNumber *rerordId;
@end
You can then use this object instead of dictionary you are using.
Contact *contact= [[Contact alloc]init];
contact.fname = (__bridge_transfer NSString *)
ABRecordCopyValue(contact, kABPersonFirstNameProperty);
contact.lname = (__bridge_transfer NSString *)
ABRecordCopyValue(contact, kABPersonLastNameProperty);
contact.recrordId = [NSNumber numberWithInt:ABRecordGetRecordID(contact)].stringValue;
[newContactList addObject:contact];
Then you can use any name in or attribute selector. Just like in your example you will write the function call sorting with fname for example like this
_tableData = [NSMutableArray arrayWithArray:
[self partitionObjects:newContactList
collationStringSelector:@selector(fname)]];
Upvotes: 0