2012-12-21 15 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>启用僵尸对象

运行项目并尝试立即崩溃。它会给你提供哪些对象导致崩溃的信息。查看该对象和您的错误将得到解决