gpt4 book ai didi

iphone - ABPeoplePicker 选择许多并返回电子邮件数组

转载 作者:可可西里 更新时间:2023-11-01 04:10:15 24 4
gpt4 key购买 nike

我想做什么:

  1. 向用户展示其设备上所有联系人中包含电子邮件地址的联系人。
  2. 允许用户在点击“完成”之前选择/取消选择任意数量的联系人。
  3. 返回电子邮件地址数组...或包含所选联系人所有联系信息的字典数组。

我尝试过的:

ABPeoplePicker 但我无法使用它来选择多个联系人。

最佳答案

我能够使用以下代码完成您所描述的操作:

#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>

@interface ELEViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end

@interface ELEViewController()
@property (nonatomic, strong) NSArray *arrayOfPeople;
@property (nonatomic, assign) CFArrayRef people;
@property (nonatomic, strong) NSMutableSet *selectedPeople;
@end

@implementation ELEViewController
@synthesize arrayOfPeople = _arrayOfPeople;
@synthesize people = _people;
@synthesize selectedPeople = _selectedPeople;

- (NSMutableSet *) selectedPeople {
if (_selectedPeople == nil) {
_selectedPeople = [[NSMutableSet alloc] init];
}
return _selectedPeople;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}

- (void)viewDidLoad {
[super viewDidLoad];

UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)
style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
ABAddressBookRef addressBook = ABAddressBookCreate();
self.people = ABAddressBookCopyArrayOfAllPeople(addressBook);
self.arrayOfPeople = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
[tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ContactCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}

int index = indexPath.row;
ABRecordRef person = CFArrayGetValueAtIndex(self.people, index);
NSString* firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
NSString* lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonLastNameProperty);
NSString *name = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

cell.textLabel.text = name;
return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arrayOfPeople.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
id person = [self.arrayOfPeople objectAtIndex:indexPath.row];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[self.selectedPeople addObject:person];
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedPeople removeObject:person];
}
NSLog(@"%@", self.selectedPeople);
}

@end

这会在 tableView 中显示整个地址簿,然后选择在联系人旁边打勾并将它们添加到 NSSet。取消选择会删除复选标记并从 NSSet 中删除条目。我将整个 ABPerson 添加到 NSSet,因此您以后仍然需要使用 C API 来处理它。

关于iphone - ABPeoplePicker 选择许多并返回电子邮件数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10402191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com