我有一个应用程序,从他们的联系人列表中选择一个人,并采取他们的名字,姓氏和电子邮件。然后,它将名字保存到nsmutablearray,并将其放入uitableview单元格。一旦在模拟器中选择了联系人,我的问题就会出现。
代码:
.h:
#import <UIKit/UIKit.h>
#import <AddressBookUI/AddressBookUI.h>
@interface FirstViewController : UIViewController < ABPeoplePickerNavigationControllerDelegate, UITableViewDelegate, UITableViewDataSource>
- (IBAction)showPicker:(id)sender;
@property (weak, nonatomic) IBOutlet NSString *firstName;
@property (weak, nonatomic) IBOutlet NSString *email;
@property (weak, nonatomic) IBOutlet NSString *lastName;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (strong, nonatomic) NSMutableArray *contacts;
@end.m:
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
@synthesize firstName;
@synthesize email;
@synthesize lastName;
@synthesize contacts;
@synthesize myTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
contacts = [[NSMutableArray alloc]initWithObjects:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableView Datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return contacts.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [contacts objectAtIndex:indexPath.row];
return cell;
}
- (IBAction)showPicker:(id)sender {
ABPeoplePickerNavigationController *picker =
[[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)peoplePickerNavigationControllerDidCancel:
(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
return NO;
}
- (void)displayPerson:(ABRecordRef)person
{
NSString* name = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonFirstNameProperty);
self.firstName = name;
NSString* last = (__bridge_transfer NSString*)ABRecordCopyValue(person,
kABPersonLastNameProperty);
self.lastName = last;
ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString *emailId = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);//0 for "Home Email" and 1 for "Work Email".
self.email = emailId;
if (!(contacts))
{
contacts = [[NSMutableArray alloc]init];
}
[contacts insertObject:firstName atIndex:0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end发布于 2014-02-20 18:40:36
UITableView必须始终与数据源保持同步。如果数据源可以在后台线程中更改,则必须特别小心。
当向数据源添加内容时,请尽快调用beginUpdate/insert/endUpdate。您不必担心缓存这些内容,当UITableView确定有足够的cpu时间和资源时,它会缓存要执行的更改。
当endUpdates被调用时,UITable将再次向dataSource询问节数和行数。如果节和行数直接从dataSource馈送,则节和行数加上插入数减删除数必须等于numberOfSections和numberOfRowsInSection的end调用返回的数字。
最后一个技巧:避免混合调用'reloadData‘和beginUpdate/endUpdate对。使用其中的一个,而不是两个都使用。
发布于 2014-02-20 18:23:25
我遇到过与此相同的问题。你所要做的就是改变
[self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];`至
[self.myTableView beginUpdates];
[self.myTableView insertRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
[self.myTableView endUpdates];来自UITableView文档
beginUpdates
Begin a series of method calls that insert, delete, or select rows and sections of the receiver.
使用beginUpdates时,必须调用endUpdates,而不是reloadData。
您可以查看https://developer.apple.com/library/ios/documentation/uikit/reference/UITableView_Class/Reference/Reference.html了解更多UITableView信息。
希望这能有所帮助!
发布于 2014-02-06 10:33:36
上面关于实现-tableView:numberOfRowsInSection:的评论是正确的。断言正在检查返回值,希望返回值会增加。
但是,由于您没有在UITableView上设置dataSource,因此它调用(或不调用)一个不存在的方法并获取0。
您需要将myTableView.dataSource和(因为您还实现了委托协议) myTableView.delegate设置为self。
您还可能需要像这样的东西
- (void)viewDidLoad
{
[super viewDidLoad];
[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
...
}除非你在其他地方注册,或者你的故事板有一个“原型单元”,它的标识符是你的代码要求的"Cell"。
https://stackoverflow.com/questions/17005635
复制相似问题