Я пытаюсь загрузить Контакты, содержащие адреса в моем TableView. Я попытался отладить его, и NSLogs показывают, что данные есть. Однако, когда я пытаюсь загрузить его в свой UITableView, он зависает на черном экране, и ничего не происходит. Отладчик показывает, что данные печатаются на консоль.My UITableView не загружает никаких данных
Я использую симулятор iOS 7 для тестирования моего проекта.
@implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
_contactsArray = [[NSMutableArray alloc] init];
[self getArrayOfPeople];
NSLog(@"array: %@", self.contactsArray);
}
return self;
}
- (void)getArrayOfPeople {
//ABAddressBookRef addressBook = ABAddressBookCreate();
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
for(CFIndex emailIndex = 0; emailIndex < nPeople; emailIndex++) {
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, emailIndex);
//ABMutableMultiValueRef emailRef= ABRecordCopyValue(person, kABPersonEmailProperty);
ABMutableMultiValueRef addressRef= ABRecordCopyValue(person, kABPersonAddressProperty);
int addressCount = (int)ABMultiValueGetCount(addressRef);
if(!addressCount) {
CFErrorRef error = nil;
ABAddressBookRemoveRecord(addressBook, person, &error);
if (error) NSLog(@"Error: %@", error);
} else {
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
NSString *contactAddress = (__bridge NSString *)ABMultiValueCopyValueAtIndex(address, 0);
NSString *name = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
if (name) {
NSMutableDictionary *contactDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"name",
contactAddress, @"address",
nil];
[self.contactsArray addObject:contactDict];
NSLog(@"%@", self.contactsArray);
}
}
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_contactsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [_contactsArray objectAtIndex:indexPath.row];
return cell;
}
@end
Надеюсь, что мой вставленный код выше поможет найти «ошибку», из-за которой мой код неисправен.
Попробуйте установить точку останова здесь return [_contactsArray count]; и посмотрите, возвращается ли счет 0. Если это так, попробуйте вызвать [tableView reloadData] в конце ViewWillAppear или в ViewDidAppear – pnavk
Может показаться глупым: вы уверены, что вы установили таблицу 'dataSource' в контроллер? – keeshux
Да, у меня был делегат и источник данных, подключенный к StoryBoard –