iphone sdk值从数组中删除给出了错误的访问权限

时间:2011-11-02 08:30:30

标签: iphone ios xcode uitableview nsmutablearray

我已经实现了这个代码,当我尝试添加对象时它工作正常但是当我尝试删除对象时它给了我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);

    }

3 个答案:

答案 0 :(得分:2)

声明你的str就像这样......

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

或写你的str @property(非原子,保留)和@synthesize,becoz一段时间字符串被自动释放,所以写保留。我认为这有助于你......

答案 1 :(得分:2)

你的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 :(得分:1)

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