【发布时间】:2015-06-23 02:29:26
【问题描述】:
所以,我正在创建一个应用程序,它有一个包含联系人的表格视图。用户从他们的联系人列表中添加联系人。
问题是;我可以添加人员,但直到重新启动后才会出现。
我已经搜索了其他问题/答案,但到目前为止没有任何帮助。
这是我的加人操作按钮:
- (IBAction)addPerson:(id)sender {
ABPeoplePickerNavigationController* picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{
[self dismissViewControllerAnimated:YES completion:nil];
}
我使用了 2 个可变数组,一个用于存储名称,另一个用于存储数字。
这里是导航控制器:
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person{
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
numbers = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"numbers"]];
people = [[NSMutableArray alloc] initWithArray:[defaults objectForKey:@"people"]];
if(people.count == 0 && numbers.count == 0){
numbers = [[NSMutableArray alloc] init];
people = [[NSMutableArray alloc] init];
}
这是表格应该自我更新的地方,但它不会。
NSString *contactName = CFBridgingRelease(ABRecordCopyCompositeName(person));
NSString* nameField = [NSString stringWithFormat:@"%@", contactName ? contactName : @"No Name"];
ABMultiValueRef phoneRecord = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFStringRef phoneNumber = ABMultiValueCopyValueAtIndex(phoneRecord, 0);
NSString* phoneField = (__bridge_transfer NSString *)phoneNumber;
CFRelease(phoneRecord);
**[tableview beginUpdates];**
[people addObject:nameField];
[numbers addObject:phoneField];
[defaults setObject:people forKey:@"people"];;
[defaults setObject:numbers forKey:@"numbers"];
[self dismissViewControllerAnimated:YES completion:nil];
//[tableview insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
//since it's a dynamic table, I'm not sure if this works.
**[tableview endUpdates];**
return NO;
}
我是否错过了其他一些 tableview 更新? 如果是这样,那应该去哪里?
我们将不胜感激任何形式的帮助。 如果我在没有 **'ed 行(更新)的情况下运行代码,它会成功添加但需要重新启动才能出现。 如果我使用带有 ** 的行或使用 beginUpdates 运行代码,我会得到 NSInternalInconsistencyException,第 0 节中的行数无效。
顺便说一句,tableview 和 tableView 是两个不同的 UITableView。
【问题讨论】:
标签: ios objective-c xcode uitableview nsarray