2013-05-17 173 views
0

我在表视图中遇到了一个问题,应用程序在我滚动并尝试选择一行时崩溃。 错误我在控制台得到的是UITableView在滚动时崩溃

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' 

请帮助我理解为什么我得到这个错误,我不认为在我的阵列中的任何零元素,让我告诉你的代码

我已经创建了一个SelectContactViewController,其中用户可以选择联系人的号码,以便这些文件是

SelectContactController.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; 

并实现文件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; 
} 

请帮我解决这个问题

+0

这甚至被称为..'[self getAddressbookData];'? –

+0

是它被调用并正常工作 –

+0

你可以通过getAddressbookData方法来获取崩溃的行吗? –

回答

0

我认为在这种情况下,调试将是很好的解决方案。所以在这条线上使用断点,让我知道它在哪个索引中崩溃。

cell.textLabel.text = [self.selectContacts objectAtIndex:indexPath.row]; 
+0

试过但没有运气 –

2

异常消息非常明确:您试图将nil值插入到数组中。该错误大概在线

[numbersArray addObject:CFBridgingRelease(phoneNumberValue)]; 

检查phoneNumberValue是否为零,然后再添加对象。另一个好主意是使用调试器来发现问题。在Xcode中运行应用程序之前添加异常断点。

+0

我没有使用数组来填充UIView表 –

+0

@PuneetBharti这个bug可能只是松散地连接到表视图。正如我所说,异常是消息是非常清楚的,你插入'nil'到数组中。带有'numbersArray'的行是发布代码中发生这种情况的唯一位置。 –

+0

你是正确的没有数值数组ArrayArray,但我的问题是与selectContact可能有零值,这就是为什么它崩溃 –

-1

它可能对于几个数字你没有提供的名字和姓氏的细节,所以你一定要试试这个:

if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst) 

     fullName = [[NSString alloc]initWithFormat:@"%@ %@", firstName, lastName]; 
    else 
     fullName = [[NSString alloc]initWithFormat:@"%@, %@", lastName, firstName]; 
    if([fullName length]>0) 

     [contactArray addObject:fullName]; 
+0

这不能是罪魁祸首,因为'stringWithFormat:'将始终返回一个非零值。 –

0

随着selectContacts = [[NSArray alloc] init];selectContacts = contactArray;设置器不使用。在分配时应该是self.selectContacts,或者是@synthesize selectContacts = _selectContacts;,然后仅在.m文件中使用_selectContacts。

+0

这与问题无关。 –