Ios 搜索文本字段结果不可选
我正在使用UITextFields作为搜索栏。我必须这样做,因为我有多个输入供人们搜索。搜索工作进行得很顺利。事实上,文本字段在内存方面比普通搜索栏工作得更好。唯一的问题是我无法选择结果单元格。当我按下电池时,什么也没发生。我甚至在didselect中放了一个log语句,而该单元格甚至没有得到该操作。请帮忙Ios 搜索文本字段结果不可选,ios,objective-c,uitableview,textfield,Ios,Objective C,Uitableview,Textfield,我正在使用UITextFields作为搜索栏。我必须这样做,因为我有多个输入供人们搜索。搜索工作进行得很顺利。事实上,文本字段在内存方面比普通搜索栏工作得更好。唯一的问题是我无法选择结果单元格。当我按下电池时,什么也没发生。我甚至在didselect中放了一个log语句,而该单元格甚至没有得到该操作。请帮忙 - (void)viewDidLoad { [super viewDidLoad]; self.ASearch.delegate = self; self.BSea
- (void)viewDidLoad
{
[super viewDidLoad];
self.ASearch.delegate = self;
self.BSearch.delegate = self;
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"Location" attributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] }];
self.ASearch.attributedPlaceholder = str;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)viewWillDisappear:(BOOL)animated {
[self dismissKeyboard];
[super viewWillDisappear:animated];
}
-(void)dismissKeyboard {
[ASearch resignFirstResponder];
[BSearch resignFirstResponder];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES];
[Asearch becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (searchArray.count == 0) {
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
messageLabel.text = @"Search!";
messageLabel.textColor = [UIColor redColor];
messageLabel.numberOfLines = 0;
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
[messageLabel sizeToFit];
self.tableView.backgroundView = messageLabel;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
return 0;
} else {
self.tableView.backgroundView = nil;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
return searchArray.count;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
headerView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 90.0, 320.0, 90.0)];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = @"searchCell";
TopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
PFObject *searchObject = [searchArray objectAtIndex:indexPath.row];
cell.AImage.image = [UIImage imageNamed:@"DefaultThumbnail"];
cell.AImage.layer.cornerRadius = 5.0f;
cell.AImage.clipsToBounds = YES;
cell.AName.text = [searchObject objectForKey:@"name"];
cell.addressLabel.text = [NSString stringWithFormat:@"%@, %@, %@",[searchObject objectForKey:@"address"],[searchObject objectForKey:@"city"], [searchObject objectForKey:@"state"]];
cell.placeType.text = [searchObject objectForKey:@"name"];
return cell;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == ASearch) {
NSString *searchString = [NSString stringWithString:placeSearch.text];
searchString = [searchString stringByReplacingCharactersInRange:range withString:string];
[self searchDataBase:searchString];
return YES;
} else {
return NO;
}
}
-(void)searchDataBase: (NSString *)subString {
PFQuery *query = [PFQuery queryWithClassName:@"HotSpots"];
[query whereKey:@"name" matchesRegex:subString modifiers:@"i"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
searchArray = [NSArray arrayWithArray:objects];
[self.tableView reloadData];
NSLog(@"%lu", objects.count);
}];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
[self dismissKeyboard];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[self searchDataBase:textField.text];
[ASearch resignFirstResponder];
[BSearch resignFirstResponder];
[self dismissKeyboard];
return YES;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier]isEqualToString:@"Segue"]) {
NSIndexPath *indexpath = [self.tableView indexPathForSelectedRow];
HSBarTableViewController *Controller = (HSBarTableViewController *)[segue destinationViewController];
PFObject *pObject = [searchArray objectAtIndex:indexpath.row];
Controller.arObject = pObject;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"Segue" sender:self];
NSLog(@"Pressed");
}
此代码是在UIViewController中还是在UITableViewController中?可能是您的点击手势识别器干扰了表视图的识别器。在dismissKeyboard方法中输入一个日志,查看在您尝试选择单元格时是否调用了该方法。是,每次我按下单元格时都会调用Dismise keyboard。我怎样才能避免这种情况?如果我注释掉点击识别器,它会工作…有没有一种方法可以实现点击呢?我知道了…我刚刚注释掉了我的Discover键盘方法,让return键处理它…再次感谢rdelmar!