由于 ex 访问不当,iphonesdk 值从数组中删除

发布于 2024-12-13 18:03:09 字数 2548 浏览 0 评论 0原文

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

    }

i had implemented this code, when i try to add objects it works fine but when i try to remove object it gave me exc_bad_access, i try to find out through putting break points but still i cannot get the reason of this. please help me.

in .h file

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);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅倚清风 2024-12-20 18:03:09

您的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];
            }   



        }






    }
}

your indexpath.row is look like to be an issue, hope this may work :

    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];
            }   



        }






    }
}
古镇旧梦 2024-12-20 18:03:09

像这样声明你的 str...

NSString *str = [[ NSString StringWithFormat:@"your string "] keep];

或编写你的 str @property (nonatomic,retain) 和 @synthesize,因为有时 String 会自动释放,因此可以写入保留。我认为这对你有帮助......

Declare your str Like this...

NSString *str = [[ NSString StringWithFormat:@"your string "] retain];

or Write your str @property (nonatomic,retain) and @synthesize, becoz some time String being autoreleased so for that be write retain. I think it helps u.....

护你周全 2024-12-20 18:03:09

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

I think this happens because you're not updating the uitableview by deleting the corresponding row. So the table view tries to load the data you've just removed and crashes. You should call DeleteRowsAtIndexPaths right after the removeObject or alternatively call reloadData at the end of the function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文