ios - Sort UITableView content (Address Book) alphabetically -
below using retrieve contacts list device. want displayed alphabetically using other examples seen on stack overflow have been unable work. code below tutorial, need sort according alphabetical order?
- (void)getpersonoutofaddressbook { //1 cferrorref error = null; abaddressbookref addressbook = abaddressbookcreatewithoptions(null, &error); if (addressbook != nil) { nslog(@"succesful."); //2 nsarray *allcontacts = (__bridge_transfer nsarray *)abaddressbookcopyarrayofallpeople(addressbook); //3 nsuinteger = 0; (i = 0; < [allcontacts count]; i++) { person *person = [[person alloc] init]; abrecordref contactperson = (__bridge abrecordref)allcontacts[i]; //4 nsstring *firstname = (__bridge_transfer nsstring *)abrecordcopyvalue(contactperson, kabpersonfirstnameproperty); nsstring *lastname = (__bridge_transfer nsstring *)abrecordcopyvalue(contactperson, kabpersonlastnameproperty); nsstring *fullname = [nsstring stringwithformat:@"%@ %@", firstname, lastname]; person.firstname = firstname; person.lastname = lastname; person.fullname = fullname; //email //5 abmultivalueref emails = abrecordcopyvalue(contactperson, kabpersonemailproperty); //6 nsuinteger j = 0; (j = 0; j < abmultivaluegetcount(emails); j++) { nsstring *email = (__bridge_transfer nsstring *)abmultivaluecopyvalueatindex(emails, j); if (j == 0) { person.homeemail = email; nslog(@"person.homeemail = %@ ", person.homeemail); } else if (j==1) person.workemail = email; } //7 [self.tabledata addobject:person]; } //8 cfrelease(addressbook); } else { //9 nslog(@"error reading address book"); } }
this uitableview code:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"identifier"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } person *person = [self.tabledata objectatindex:indexpath.row]; cell.textlabel.text = person.fullname; return cell; }
i have tried below
[self.tabledata sortusingselector:@selector(localizedcaseinsensitivecompare:)];
i have tried nssortdescriptor
don't have key sort by.
you'll need sort array of person
objects. once have finished adding them array can sort on fullname
using following code:
[self.tabledata sortusingcomparator:^nscomparisonresult(person *p1, person *p2) { return [p1.fullname compare:p2.fullname]; }];
alternative
you may want implement compare:
method on person
object , perform comparison there, keep sorting logic nicely encapsulated , ensure else uses person
objects can perform sorts without duplicating code shown above.
@implementation person // implementation contain more code, not shown brevity - (nscomparisonresult)comparebyfullname:(person *)otherperson { return [self.fullname compare:otherperson.fullname]; } @end
then can sort array with:
[self.tabledata sortusingselector:@selector(comparebyfullname:)];
Comments
Post a Comment