我正在尝试在 Xcode 上执行联系人应用程序,联系人列表显示,但是当我单击任何名称时,它会引发错误 EXC_BAD_ACCESS,我检查了 NSLog 并发现我的数组在填充它自己的 tableview 时出错,这是我的代码:
//数组初始化为
@property (strong,nonatomic) NSMutableArray *filteredData,*contactAdd;
//在这个函数上填充的数组
-(void)reloadAddressBook
{
self.contactAdd = [[NSMutableArray alloc]init];
ABAddressBookRef addressBook = ABAddressBookCreate();//ABAddressBookCreateWithOptions(NULL,NULL);
if(ABAddressBookHasUnsavedChanges(addressBook))
{
ABAddressBookSave(addressBook,NULL);
}
NSMutableArray *allPeople = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
int nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i < nPeople; i++ )
{
ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
[self.contactAdd addObject:(__bridge id)(person)];
NSLog(@"@details %@",contactAdd);
CFRelease(person);
}
CFRelease(addressBook);
}
//这个函数出错
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if(isFiltered)
{
ABRecordRef person = (__bridge ABRecordRef)[self.filteredData objectAtIndex:indexPath.row];
NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[cell.textLabel setText:tweet];
CFRelease(person);
}
else
{
ABRecordRef person = (__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);
NSLog(@"%@",person);
NSString *tweet=[[NSString stringWithFormat:@"%@", ABRecordCopyValue(person, kABPersonFirstNameProperty)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[cell.textLabel setText:tweet];
CFRelease(person);
NSLog(@"%@",indexPath);
// I have 4 contacts currently and error occurs here on 4th time and when I continue error occurs on 2nd time
NSLog(@"@details %@",contactAdd);
}
return cell;
}
//在运行时这里出错
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ABPersonViewController *currentPersonView=[[ABPersonViewController alloc]init];
ABRecordRef person;
if(isFiltered)
person=(__bridge ABRecordRef)([self.filteredData objectAtIndex:indexPath.row]);
else
person=(__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);//error here
currentPersonView.displayedPerson=person;
currentPersonView.allowsEditing=YES;
currentPersonView.allowsActions=YES;
[self.navigationController pushViewController:currentPersonView animated:YES];
}
//NSLog
2013-08-01 13:11:12.715 联系人[7722:207] 2 个索引 [0, 0]
当前语言:自动;目前objective-c 2013-08-01 13:11:26.614 Contacts[7722:207] @details ("CPRecord: 0xba1b340 ABPerson", "CPRecord: 0xba1c870 ABPerson", "CPRecord: 0xba1c160 ABPerson", "CPRecord: 0xba1c990 ABPerson" )
2013-08-01 13:11:26.616 联系人[7722:207]
2013-08-01 13:11:26.617 联系人[7722:207] 2 个索引 [0, 1]
2013-08-01 13:11:27.566 联系人[7722:207] @details(“CPRecord:0xba1b340 ABPerson”,“CPRecord:0xba1c870 ABPerson”,“CPRecord:0xba1c160 ABPerson”,“CPRecord:0xba1c990 ABPerson”)201 -01 13:11:27.567 联系人[7722:207] 2013-08-01 13:11:27.568 联系人[7722:207] 2 个索引 [0, 2] (gdb)