Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取表视图上的EXC\u BAD\u访问权限_Ios_Objective C_Out Of Memory - Fatal编程技术网

Ios 获取表视图上的EXC\u BAD\u访问权限

Ios 获取表视图上的EXC\u BAD\u访问权限,ios,objective-c,out-of-memory,Ios,Objective C,Out Of Memory,我正在尝试在Xcode上运行contacts app,联系人列表会显示,但当我单击任何名称时,它会抛出错误EXC_BAD_ACCESS,我已使用NSLog进行检查,发现我的数组在填充tableview时出错,这是我的代码: //数组初始化为 @property (strong,nonatomic) NSMutableArray *filteredData,*contactAdd; //在此函数上填充的数组 -(void)reloadAddressBook { self.contactA

我正在尝试在Xcode上运行contacts app,联系人列表会显示,但当我单击任何名称时,它会抛出错误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]

当前语言:自动;当前目标-c 2013-08-01 13:11:26.614联系人[7722:207]@详情( “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]@详情( “CPRecord:0xba1b340 ABPerson”, “CPRecord:0xba1c870 ABPerson”, “CPRecord:0xba1c160 ABPerson”, “CPRecord:0xba1c990 ABPerson” ) 2013-08-01 13:11:27.567联系人[7722:207] 2013-08-01 13:11:27.568联系人[7722:207]2个索引[0,2]
(gdb)

首先为您的
单元标识符输入一个值:


static NSString*cellIdentifier=@“cellIdentifier
r”

不要离开
静态NSString*cellIdentifier不带值,例如:

static NSString *cellIdentifier = @"SimpleTableCell"
单元格标识符添加到.m文件后,请确保已在情节提要中为tableView单元格添加了一个标识符

请参见此图:


(很抱歉,在进一步了解stackoverflow之前,我无法嵌入图像!)

您需要了解您的
contactAdd
阵列是如何管理的。创建您的
contactAdd
数组的目的是填充100个对象,但您只有四个联系人

self.contactAdd = [[NSMutableArray alloc]initWithCapacity:100];
这应该只填充实际存在的联系人数量。但您也将对象添加到阵列中,而不考虑最初的100个插槽

[self.contactAdd addObject:(__bridge id)(person)];
现在,当您选择一个人时,可能没有实际使用的对象,正如您在自己的评论中所指出的那样

person=(__bridge ABRecordRef)([self.contactAdd objectAtIndex:indexPath.row]);//error here
根据您的代码,只需创建数组,然后让填充自然发生:

self.contactAdd = [[NSMutableArray alloc]init];
此外,在人口增长过程中,有一些事情是没有意义的。首先添加person对象,然后移动person对象

[self.contactAdd addObject:(__bridge id)(person)];
[self.contactAdd replaceObjectAtIndex:i withObject:(__bridge id)(person)];
我猜这与原来的100个插槽有关,但是删除这个神奇的数字也会删除这个步骤

此外,您正在释放一个未保留的对象:

ABRecordRef person = (__bridge ABRecordRef)([allPeople objectAtIndex:i]);
//. . . 
CFRelease(person);
我认为您需要查看有关使用表视图的文档,以便更好地理解这里要完成的工作。但是,要考虑的一件事就是这样做…

self.contactAdd = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
…因为您正在使用给定的对象


前两个答案也是正确的,您的
cellIdentifier
在创建时需要填充一个字符串。

我能够使用CFArray解决这个问题,下面是代码,以防其他人需要它。(iphone模拟器5.0)


我的故事板中只有一个视图、表视图和搜索栏,还没有添加表视图单元格您的表视图需要单元格,静态还是自定义?当您使用以下方法的信息填充表时:
-(UITableViewCell*)tableView:(UITableView*)tableView cellforrowatinexpath:(nsindepath*)indepath
,它返回一个
单元格
。在故事板的tableview中添加一个单元格重新阅读您的问题,当您启动应用程序时-它是否加载联系人列表?一旦它们被加载到tableView中,是否就是在您选择其中一个联系人时收到错误?如果是,那么我们需要查看您的
didselectrowatinex
方法是的,当我单击其中一个联系人时收到错误,我在问题中添加了我的didselectrowatinex。我添加initwithcapacity只是为了检查问题,最初是jst init,我忘了在这里注释replaceobject和CF release,这是在我调试该问题时完成的。我认为在调试时添加这两件事弊大于利。
-(void)reloadAddressBook
{

    if(addressBook)
        CFBridgingRelease(addressBook);
    addressBook = ABAddressBookCreate();
    person=ABAddressBookCopyDefaultSource(addressBook);
    contactAdd=ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(self.addressBook,self.person,0);
    }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSString *cellIdentifier;

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];


        if (cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
           }

        if(isFiltered)
        {
           self.person = CFArrayGetValueAtIndex(self.filteredData,indexPath.row);
            NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

            [cell.textLabel setText:tweet];


        }
        else
        {
            self.person = CFArrayGetValueAtIndex(self.contactAdd, indexPath.row);
            NSString *tweet=[[NSString stringWithFormat:@"%@",(__bridge_transfer NSString *)ABRecordCopyCompositeName(self.person)] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
             [cell.textLabel setText:tweet];           
        }


        return cell;
    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(isFiltered)
       return CFArrayGetCount(self.filteredData);
    else
    {
        if(CFArrayGetCount(self.contactAdd)==0)
        {
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Message"
                                                              message:@"Empty Contacts"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil];
            [message show];
        }

        return CFArrayGetCount(self.contactAdd);
    }
    }