удаление значения iphonesdk из массива, если не указан плохой доступ - PullRequest
2 голосов
/ 02 ноября 2011

Я реализовал этот код, когда я пытаюсь добавить объекты, он работает нормально, но когда я пытаюсь удалить объект, он дает мне 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 ]

2 голосов
/ 02 ноября 2011

Объявите свою стрсохраните) и @synthesize, потому что некоторое время String автоматически высвобождается, так что для этого необходимо сохранить запись.Я думаю, что это помогает тебе .....

2 голосов
/ 02 ноября 2011

Ваш 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];
            }   



        }






    }
}
1 голос
/ 02 ноября 2011

Я думаю, это происходит потому, что вы не обновляете uitableview, удаляя соответствующую строку. Поэтому табличное представление пытается загрузить только что удаленные данные и вылетает. Вы должны вызвать DeleteRowsAtIndexPaths сразу после removeObject или альтернативно вызвать reloadData в конце функции.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...