我想搜索数组中的数据并刷新表视图,但是当我调用延迟函数时,我得到了 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