udało mi się zrobić to, co myślę opisujesz z następującego kodu:
#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
To pokazuje całą książkę adresową w Tableview, a następnie wybierając stawia znacznik wyboru obok kontaktu i dodaje je do NSSet. Odznaczenie usuwa znacznik wyboru i usuwa wpis z NSSet. Dodaję cały ABPerson do NSSet, więc będziesz musiał używać C API, aby pracować z nim później.
Dokładnie tego potrzebowałem. Jestem w stanie dodać użytkowników do 'NSSet' stamtąd i upewnić się, że są one dodane tylko raz. Jedną rzeczą jest to, że użytkownik nic nie wskazuje na to, że coś się stało ... czy istnieje sposób dodania "zaznaczenia" lub czegoś obok wiersza po zaznaczeniu i usunięcia go, jeśli nie jest zaznaczony? –
Nie jestem tego pewien. Zastanawiałem się. Myślałem, że możesz uzyskać dostęp do kontrolera widoku tabeli, ale nie wydaje się, że jest on odsłonięty. Kontroler selektora osób jest podklasą kontrolera nawigacyjnego. Jeśli chcesz, aby było bardziej oczywiste, może to wymagać więcej pracy. –
Myślę, że będę musiał użyć 'ABAddressBookCopyArrayOfAllPeople', a następnie stwórz niestandardowy widok TableView, aby zrobić to, co chcę zrobić. Dam ci znać, co wymyśliłem. –