在我的应用程序中,我有按升序显示的联系人列表。当用户点击任何字母表'b'时,列表应滚动到从'b'开始的联系人。这是内置的功能地址簿?谁知道我怎么能做到这一点?
提前致谢!
答案 0 :(得分:1)
我很脏的方法。它通过电子邮件排序,名字和姓氏省略中间名,因为我不需要那个。哦,它只找到那些有电子邮件地址的联系人。如果您稍微编辑以if (ABMultiValueGetCount(emailRef))
您的视图控制器:
- (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
卫生署!我没有保持警惕,仔细阅读整件事。 :)尝试创建NSMutableDictionary,并且每次调用headerForSection:方法时,在字典中存储它的偏移量,并以相应的字母作为键。然后,当用户选择“B”字母时,发送你的UITableView setContentOffset:animated:方法,并从该字典中获取适当的偏移量。