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
4

1 回答 1

2

请转到产品>编辑方案>对角器>启用僵尸对象

运行项目并尝试立即崩溃。它将为您提供有关哪个对象导致崩溃的信息。查看该对象,您的错误将得到解决

于 2012-12-21T12:57:48.180 回答