2012-12-21 12 views
0

我想搜索數組中的數據並刷新表視圖,但是當我調用延遲功能時,我得到了BAD ACCESS異常並且無法工作。 但我想這個功能有一定的延遲後,這裏是我的代碼,這是我的完整的類我得到了不好的訪問異常,當延遲調用功能

#import "ViewController.h" 
    #import "DBHandler.h" 
    #import "placeDC.h" 

    @interface ViewController() 

    @end 

    @implementation ViewController 

    - (void)viewDidLoad 
    { 
     [super viewDidLoad]; 


     DBHandler *db = [[DBHandler alloc]init]; 
     dataArray=[db autoComplete]; 
     isSearching=0; 

     myTableView.hidden=YES; 
     // Do any additional setup after loading the view, typically from a nib. 

     dispalyPlaces=[[NSMutableArray alloc] initWithArray:dataArray]; 
    } 

    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 



    #pragma mark - Table view data source 

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return 1; 
    } 

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     if(isSearching) 
     { 
      return [dispalyPlaces count]; 
     } 
     else 
      return [dataArray count]; 
    } 

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
     return 44; 
    } 



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 

     /* FOR SIMPLE CELL */ 
     static NSString *MyIdentifier = @"MyCell"; 

     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier]; 
     placeDC *palcedc; 

     // NSLog(@"%d",isSearching); 
     if(isSearching) 
     { 
      palcedc = [dispalyPlaces objectAtIndex:[indexPath row]]; 
     } 
     else 
     { 
      palcedc = [dataArray objectAtIndex:[indexPath row]]; 
     } 
     cell.textLabel.text = palcedc.placeNmae; 

      return cell; 
    } 


    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
    { 




    } 

    -(void)delayCall{ 
     for (placeDC *placedc in dataArray) { 
      NSRange r ; 

      r = [placedc.placeNmae rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
        if (r.location!=NSNotFound) { 
       [dispalyPlaces addObject:placedc]; 
      } 
     } 
     } 

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    { 
     [self.view endEditing:YES]; 
    } 

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ 
     if ([string length]==0) { 

      [dispalyPlaces removeAllObjects]; 
      [dispalyPlaces addObjectsFromArray:dataArray]; 
      isSearching = 0; 

      myTableView.hidden=YES; 
     } 
     else{ 
      isSearching=1; 
      searchText=textField.text; 
     [dispalyPlaces removeAllObjects]; 
      myTableView.hidden=NO; 

      [self performSelector:@selector(delayCall) withObject:nil afterDelay:0.1]; 

      // [self delayCall]; 

    //  for (placeDC *placedc in dataArray) { 
    //   NSRange r ; 
    //   NSLog(@"%@",placedc.placeNmae); 
    //    
    //    
    //  r = [placedc.placeNmae rangeOfString:textField.text options:NSCaseInsensitiveSearch]; 
    //  
    //    
    //   if (r.location!=NSNotFound) { 
    //    [dispalyPlaces addObject:placedc]; 
    //   } 
    //  } 
      [myTableView reloadData]; 

     } 
     NSLog(@"%d",[dispalyPlaces count]); 
     return YES; 

    } 


    //- (void)searchBar:(UITextField *)searchBar textDidChange:(NSString *)searchText{ 
    // 
    // if ([txtSearch.text length]==0) { 
    //  myTableView.hidden=YES; 
    // } 
    // 
    // else{ 
    //  myTableView.hidden=NO; 
    //  [dispalyPlaces removeAllObjects]; 
    //  for (placeDC *palce in dataArray) { 
    //   NSRange r ; 
    //    
    //   r = [palce.placeNmae rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
    //  } 
    // 
    // } 
    //  
    //} 
    // 
    //- (void)dealloc { 
    // [myTableView release]; 
    // [txtSearch release]; 
    // [super dealloc]; 
    //} 
    @end 
+0

顯示Ivars以及如何初始化它們。 –

+0

請顯示崩潰報告,特別是代碼崩潰的行。 – Codo

+0

io編輯問題plz回放 –

回答

2

請到產品>編輯舍姆> diagonist>啓用殭屍對象

運行項目並嘗試立即崩潰。它會給你提供哪些對象導致崩潰的信息。查看該對象和您的錯誤將得到解決