我正在使用UITextFields作为我的搜索栏。我不得不这样做,因为我有多个输入供人们搜索。搜索工作完美。事实上,文本字段比普通的搜索栏更好地工作。唯一的问题是我无法选择生成的单元格。当我按下电池时什么也没发生。我甚至在didselect中放了一个日志声明,单元格甚至没有得到动作。请帮助。
- (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");
}