2011-11-02 50 views
2

我已經實現了這個代碼,當我嘗試添加它正常工作的對象,但是當我嘗試刪除對象時,它給我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對象 –

回答

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



     } 






    } 
} 
1

我想這是因爲你沒有通過刪除相應的行更新的UITableView。所以表視圖會嘗試加載剛刪除和崩潰的數據。你應該在removeObject之後調用DeleteRowsAtIndexPaths,或者在函數結尾調用reloadData。

2

聲明你的STR就像這個...

的NSString *海峽= [[NSString的StringWithFormat:@ 「你的字符串」]保留];

或撰寫您的STR @屬性(非原子,保留)和@synthesize,becoz一段時間字符串被自動釋放,以便爲被寫保留。我認爲它有助於ü.....