【问题标题】:iphonesdk value removing from arrray given exc bad accessiphone sdk 值从给定 exc 错误访问的数组中删除
【发布时间】:2011-11-02 08:30:29
【问题描述】:

我已经实现了这段代码,当我尝试添加对象时它工作正常,但是当我尝试删除对象时它给了我 exc_bad_access,我尝试通过放置断点来找出原因,但我仍然无法得到原因。请帮帮我。

在 .h 文件中

BOOL prayValues[1000];
BOOL praiseValues[1000];
IBOutlet UITableView *prayTable;
IBOutlet UITableView *praiseTable;
NSMutableArray *publishingMyPosts;




- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    //NSLog(@"sterrrr----%@",str);

    if (tableView == myTableView) {
    NSLog(@"did select in tableview");
    }



    if (jsonRequestChecking==2) {

        UITableViewCell *thisCell1 = [prayTable cellForRowAtIndexPath:indexPath];
        UITableViewCell *thisCell2 = [praiseTable cellForRowAtIndexPath:indexPath];

    if (tableView == prayTable) {
        NSLog(@"did select in prayTable");      
        prayValues[indexPath.row] = !prayValues[indexPath.row];

        if (prayValues[indexPath.row]) { 
            thisCell1.accessoryType = UITableViewCellAccessoryCheckmark;
            NSLog(@"this cell1 text %@",thisCell1.textLabel.text);
            str=[NSString stringWithFormat:@"%@",[[publicArray objectAtIndex:indexPath.row] objectForKey:@"id"]];

            if (([publishingMyPosts containsObject:str] == NO)){

                [publishingMyPosts addObject:str];
            //  NSLog(@"publishing my post %@",publishingMyPosts);


            }

        } else {
            thisCell1.accessoryType = UITableViewCellAccessoryNone;

            [publishingMyPosts  removeObject:str];
            //NSLog(@"publishing my post %@",publishingMyPosts);

        }



        }



    if (tableView == praiseTable) {
        NSLog(@"did select in praiseTable");
        praiseValues[indexPath.row] = !praiseValues[indexPath.row];
        if (praiseValues[indexPath.row]) { 
            str1=[NSString stringWithFormat:@"%@",[[privateArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
            thisCell2.accessoryType = UITableViewCellAccessoryCheckmark;
            NSLog(@"this cell2 text %@",thisCell2.textLabel.text);

            if (([publishingMyPosts containsObject:str1] == NO)){
                [publishingMyPosts addObject:str1];
            //  NSLog(@"publishing my post %@",publishingMyPosts);


            }

        } else {
            thisCell2.accessoryType = UITableViewCellAccessoryNone;
            [publishingMyPosts  removeObject:str1];
            //NSLog(@"publishing my post %@",publishingMyPosts);

        }

        }
    }

    NSLog(@"publishing my post %@",publishingMyPosts);

    }

【问题讨论】:

  • 认为这一定与您的 str 在调用之间被自动释放有关吗?还在看。
  • 你确定你的应用程序由于从数组中删除了 obj 而崩溃了吗???
  • 是的。我敢肯定,当我尝试删除对象时,它给了我非常糟糕的访问权限,并且它随机有时会删除 3 个对象,然后在删除 2 个对象时给我非常糟糕的访问权限和一些时间
  • NSString * str; NSString * str1;在具有非原子属性的 .h 文件中声明并保留和即时释放它。
  • 您在代码中使用的是[str release] 还是[str1 release]`????

标签: iphone ios xcode uitableview nsmutablearray


【解决方案1】:

您的 indexpath.row 似乎是个问题,希望这可能有效:

    if (indexPath.row < [publicArray count] ) {
    str=[NSString stringWithFormat:@"%@",[[publicArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
    NSLog(@"str === %@",str);
    }

    if (indexPath.row < [privateArray count] ){ 
    str1=[NSString stringWithFormat:@"%@",[[privateArray objectAtIndex:indexPath.row] objectForKey:@"id"]];
    NSLog(@"str1 === %@",str1);
}
    if (tableView == myTableView) {
    NSLog(@"did select in tableview");
    }



    if (jsonRequestChecking==2) {

        UITableViewCell *thisCell1 = [prayTable cellForRowAtIndexPath:indexPath];
        UITableViewCell *thisCell2 = [praiseTable cellForRowAtIndexPath:indexPath];

    if (tableView == prayTable) {

        prayValues[indexPath.row] = !prayValues[indexPath.row];
        if (prayValues[indexPath.row]) { 
            thisCell1.accessoryType = UITableViewCellAccessoryCheckmark;


            if (([publishingMyPosts containsObject:str] == NO)){

                [publishingMyPosts addObject:str];

            }

            }

        else {
            thisCell1.accessoryType = UITableViewCellAccessoryNone;

            [publishingMyPosts  removeObject:str];
        }   



    }



        if (tableView == praiseTable) {

            praiseValues[indexPath.row] = !praiseValues[indexPath.row];
            if (praiseValues[indexPath.row]) { 
                thisCell2.accessoryType = UITableViewCellAccessoryCheckmark;


                if (([publishingMyPosts containsObject:str1] == NO)){

                    [publishingMyPosts addObject:str1];

                }

            }

            else {
                thisCell2.accessoryType = UITableViewCellAccessoryNone;

                [publishingMyPosts  removeObject:str1];
            }   



        }






    }
}

【讨论】:

    【解决方案2】:

    像这样声明你的 str...

    NSString *str = [[ NSString StringWithFormat:@"你的字符串"] 保留];

    或者写你的str @property (nonatomic,retain) 和@synthesize,因为有时候String 会被自动释放,所以写retain。我认为它可以帮助你.....

    【讨论】:

      【解决方案3】:

      我认为这是因为您没有通过删除相应的行来更新 uitableview。因此,表视图会尝试加载您刚刚删除的数据并崩溃。您应该在 removeObject 之后立即调用 DeleteRowsAtIndexPaths,或者在函数结束时调用 reloadData。

      【讨论】:

        猜你喜欢
        • 2011-08-07
        • 1970-01-01
        • 1970-01-01
        • 2011-08-30
        • 2015-04-11
        • 2016-07-19
        • 2011-08-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多