Iphone UITableView在滚动时崩溃
我在表视图中遇到一个问题,当我向下滚动并尝试选择一行时,应用程序就会崩溃。 我在控制台中遇到的错误是Iphone UITableView在滚动时崩溃,iphone,uitableview,Iphone,Uitableview,我在表视图中遇到一个问题,当我向下滚动并尝试选择一行时,应用程序就会崩溃。 我在控制台中遇到的错误是 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 请帮助我理解为什么会出现这个错误,我不认为我的数组中有任何nil元素,让我向您展示代码 我已经创建了一个Se
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
请帮助我理解为什么会出现这个错误,我不认为我的数组中有任何nil元素,让我向您展示代码
我已经创建了一个SelectContactViewController,用户可以在其中选择联系人的数量,以便保存文件
选择ContactController.h
#import <UIKit/UIKit.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface SelectContactController :UIViewController
<UITableViewDataSource, UITableViewDelegate>
{
NSArray *selectedImageIds;
NSArray *selectContacts;
}
@property (nonatomic, retain) NSArray *selectContacts;
#导入
#进口
#进口
@界面选择ContactController:UIViewController
{
NSArray*选定的图像;
NSArray*选择联系人;
}
@属性(非原子,保留)NSArray*selectContacts;
以及实现文件SelectContactController.m
#import "SelectContactController.h"
@interface SelectContactController ()
@end
@implementation SelectContactController
@synthesize selectContacts;
- (void)viewDidLoad
{
[super viewDidLoad];
selectContacts = [[NSArray alloc] init];
selectedRows = [[NSMutableArray alloc] init];
contactDictionary = [[NSMutableDictionary alloc] init];
id appDelegate = (id)[[UIApplication sharedApplication] delegate];
managedObjectContext = [appDelegate managedObjectContext];
selectedImageIds = [appDelegate selectedImageIds];
[self getAddressbookData];
}
#pragma mark - Address book Methods
-(void)getAddressbookData
{
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
ABAddressBookRef addressBook = ABAddressBookCreate();
#else
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
#endif
NSArray * people;
BOOL accessGranted = [self __addressBookAccessStatus:addressBook];
if (accessGranted) {
people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
// Do whatever you need with thePeople...
}
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *contactArray = [[NSMutableArray alloc] init];
for (CFIndex i = 0; i < nPeople; i++) {
ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i);
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
NSString *fullName = nil;
if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
else
fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName];
[contactArray addObject:fullName];
//
// Phone Numbers
//
ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );
NSMutableArray *numbersArray = [[NSMutableArray alloc] init];
for ( CFIndex k=0; k<phoneNumberCount; k++ )
{
CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
// converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"
// Find the ones you want here
//
// NSLog(@"-----PHONE ENTRY -> name:%@ : %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue );
[numbersArray addObject:CFBridgingRelease(phoneNumberValue)];
CFRelease(phoneNumberLocalizedLabel);
CFRelease(phoneNumberLabel);
CFRelease(phoneNumberValue);
}
// NSLog(@"phone numbers %@", numbersArray);
[contactDictionary setObject:numbersArray forKey:fullName];
CFRelease(record);
}
selectContacts = contactArray;
// NSLog(@"dictionary of array %@", contactDictionary);
//NSLog(@"contacts count %d", [selectContacts count]);
}
-(BOOL)__addressBookAccessStatus:(ABAddressBookRef) 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);
// dispatch_release(sema);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
return accessGranted;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"select content count %d", [selectContacts count]);
return [selectContacts 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 = [self.selectContacts objectAtIndex:indexPath.row];
return cell;
}
#导入“SelectContactController.h”
@接口选择ContactController()
@结束
@控制器的实现
@综合选择触点;
-(无效)viewDidLoad
{
[超级视图下载];
选择Contacts=[[NSArray alloc]init];
selectedRows=[[NSMutableArray alloc]init];
contactDictionary=[[NSMutableDictionary alloc]init];
id appDelegate=(id)[[UIApplication sharedApplication]委托];
managedObjectContext=[appDelegate managedObjectContext];
SelectedImageId=[appDelegate SelectedImageId];
[self-getAddressbookData];
}
#pragma标记-地址簿方法
-(void)getAddressbookData
{
#如果IPHONE操作系统版本最低要求<60000
ABAddressBookRef addressBook=ABAddressBookCreate();
#否则
CFErrorRef*错误=nil;
ABAddressBookRef addressBook=ABAddressBookCreateWithOptions(空,错误);
#恩迪夫
NSArray*人;
BOOL accessgrated=[self\uu addressBookAccessStatus:addressBook];
如果(已授予访问权限){
人员=(uuuu bridge_utransfer NSArray*)abAddressBookCopyArrayFallPeople(addressBook);
//对人民做你需要的一切。。。
}
CFIndex nppeople=ABAddressBookGetPersonCount(地址簿);
NSMutableArray*contactArray=[[NSMutableArray alloc]init];
对于(CFIndex i=0;i 对于(CFIndex k=0;k我认为在这种情况下,调试将是一个很好的解决方案。因此,在这一行中使用断点,并让我知道它在哪个索引中崩溃
cell.textLabel.text = [self.selectContacts objectAtIndex:indexPath.row];
异常消息非常清楚:您正试图将nil
值插入数组中。错误可能就在这一行
[numbersArray addObject:CFBridgingRelease(phoneNumberValue)];
在添加对象之前,请检查phoneNumberValue
是否为nil。另一个好办法是使用调试器查找问题。在Xcode中运行应用程序之前,请添加一个异常断点。对于少数数字,您可能没有提供名字和姓氏详细信息,因此您必须尝试以下操作:
if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
fullName = [[NSString alloc]initWithFormat:@"%@ %@", firstName, lastName];
else
fullName = [[NSString alloc]initWithFormat:@"%@, %@", lastName, firstName];
if([fullName length]>0)
[contactArray addObject:fullName];
使用selectContacts=[[NSArray alloc]init]
或selectContacts=contactArray;
不使用setter。它应该是self。分配时选择contacts
,或者执行@synthesis-selectContacts=\u-selectContacts;
然后仅在.m文件中使用_-selectContacts。这是否被调用..[self getAddressbookData];
?是的,它被调用并且工作正常。您可以单步通过方法getAddressbookData
获取它崩溃的行吗?这不是罪魁祸首,因为stringWithFormat:
将始终返回非空值。我没有使用数字数组填充UIViewtable@PuneetBharti这个bug可能只是松散连接正如我所说,例外是消息非常清楚,您正在将nil
插入数组中。带有numbersArray
的行是发布代码中唯一可能发生这种情况的地方。您是对的,numbersArray中有nil值,但我的问题是selectContact可能有nil值,这就是为什么它的cRashin在您发布的代码中,无法尝试将nil值插入到contactArray
中。这与问题无关。