开发者

How to implement sort functionality same as AddressBook?

开发者 https://www.devze.com 2023-04-06 15:03 出处:网络
In my app, I have list of contacts which are displayed in ascending order.When user clicks on any alphabet say \'b\' then the list should scrolls to the contact starting from \'b\'.Is this built-In fu

In my app, I have list of contacts which are displayed in ascending order.When user clicks on any alphabet say 'b' then the list should scrolls to the contact starting from 'b'.Is this built-In functional开发者_Python百科ity of AddressBook?Can anyone knows how I can achieve this?

Thanks in advance!


My pretty dirty method. It sorts by email, first name and last name omitting middle name cause I didn't needed that one. Oh and it finds only those contacts which have email address. You can avoid that if you slightly edit code starting with if (ABMultiValueGetCount(emailRef))

Your view controller:

    - (NSArray *)sortedContactsFromPeople:(CFArrayRef)people {
  NSMutableArray *contacts = [NSMutableArray array];

  for (int i = 0; i < CFArrayGetCount(people); i++) {
    ABRecordRef record = CFArrayGetValueAtIndex(people, i);
    ABMultiValueRef emailRef = ABRecordCopyValue(record, kABPersonEmailProperty);
    CFStringRef email;

    if (ABMultiValueGetCount(emailRef)) {

      BOOL hasValidEmail = NO;

      for (int j = 0; j < ABMultiValueGetCount(emailRef); j++) {
        if (!hasValidEmail) {
          email = ABMultiValueCopyValueAtIndex(emailRef, j);
          if ([Validator validateEmail:(NSString *)email] == kValNoErr)
            hasValidEmail = YES;
          else
            CFRelease(email);
        }
      }

      if (hasValidEmail) {
        CFStringRef name = ABRecordCopyValue(record, kABPersonFirstNameProperty);        
        CFStringRef lastname = ABRecordCopyValue(record, kABPersonLastNameProperty);

        NSData *contactImageData = (NSData*)ABPersonCopyImageData(record);
        UIImage *img = [[[UIImage alloc] initWithData:contactImageData] autorelease];
        [contactImageData release];

        if (lastname == nil)
          lastname = (CFStringRef)@"";

        if (name == nil)
          name = (CFStringRef)@"";

        Contact *contact = [[[Contact alloc] initWithName:(NSString *)name
                                                 lastname:(NSString *)lastname
                                                    email:(NSString *)email
                                              profileIcon:img] autorelease];

        if (![(NSString *)lastname isEqualToString:@""])
          contact.sortChar = [(NSString *)lastname substringToIndex:1];
        else if (![(NSString *)name isEqualToString:@""])
          contact.sortChar = [(NSString *)name substringToIndex:1];
        else if (![(NSString *)email isEqualToString:@""])
          contact.sortChar = [(NSString *)email substringToIndex:1];                 

        contact.idNumber = ABRecordGetRecordID(record);

        [contacts addObject:contact];

        if (lastname)
          CFRelease(lastname);
        if (name)
          CFRelease(name);
        CFRelease(email);
      }
    }
    CFRelease(emailRef);
  }

  NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"sortChar" ascending:YES selector:@selector(caseInsensitiveCompare:)];
  [contacts sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
  return contacts;
}

- (void)initBaseValues {
  sections = [[NSMutableDictionary alloc] init];

  ABAddressBookRef addressBook = ABAddressBookCreate();

  CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);

  NSInteger section = 0;
  NSString *prevChar = nil;

  NSArray *contacts = [self sortedContactsFromPeople:people];

  for (int i = 0; i < contacts.count; i++) {

    Contact *contact = [contacts objectAtIndex:i];

    BOOL sectionExists = NO;

    if ([prevChar isEqualToString:contact.sortChar])
      sectionExists = YES;

    if (!sectionExists) {
       [sections setObject:[NSMutableArray array] forKey:[NSString stringWithFormat:@"%d", section]];
       section++;
    }

    [prevChar autorelease];
    prevChar = [contact.sortChar copy];

    [[sections objectForKey:[NSString stringWithFormat:@"%d", section-1]] addObject:contact];
  }

  if (prevChar != nil)
    [prevChar release];

  CFRelease(people);
  CFRelease(addressBook);
}

Contact.h

@interface Contact : NSObject {
  NSString *name;
  NSString *lastname;
  NSString *email;
  UIImage  *profileIcon;
  NSInteger idNumber;
}

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *lastname;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, retain) UIImage *profileIcon;
@property (nonatomic) NSInteger idNumber;
@property (nonatomic, copy) NSString *sortChar;

- (id)initWithName:(NSString *)name_
          lastname:(NSString *)lastname_
             email:(NSString *)email_
       profileIcon:(UIImage *)profileIcon_;

@end

Doh! I wasn't vigilant enough, to read the whole thing carefully. :) Try creating NSMutableDictionary and each time headerForSection: method is being called store it's offset in the dictionary with appropriate letter as key. Then when user selects "B" letter send your UITableView setContentOffset:animated: method with appropriate offset taken from that dictionary.

0

精彩评论

暂无评论...
验证码 换一张
取 消