iphonesdk值从arrray给出EXC坏访问

iphonesdk值从arrray给出EXC坏访问

问题描述:

我已经实现了这个代码,当我尝试添加它正常工作的对象,但是当我尝试删除对象时,它给我EXC_BAD_ACCESS,我试图通过把破发点,但还是以找出消除我无法得到这个原因。请帮帮我。iphonesdk值从arrray给出EXC坏访问

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

    } 
+0

认为它必须与通话之间自动释放你的海峡办?仍在通过它看。 – wmorrison365

+0

你确定你的应用程序崩溃了,因为从数组中删除了obj? –

+0

yap。我敢肯定,它给了我不好的访问时,当我尝试删除对象,它的随机一些时间它删除3个对象,然后给我不好的访问和一些时间,同时删除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]; 
      } 



     } 






    } 
} 

我想这是因为你没有通过删除相应的行更新的UITableView。所以表视图会尝试加载刚删除和崩溃的数据。你应该在removeObject之后调用DeleteRowsAtIndexPaths,或者在函数结尾调用reloadData。

声明你的STR就像这个...

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

或撰写您的STR @属性(非原子,保留)和@synthesize,becoz一段时间字符串被自动释放,以便为被写保留。我认为它有助于ü.....