我遇到了一个大问题。我有一个textfield
我正在做自动完成功能,以显示内部tappe时的项目列表。我在tableView
(自动完成tableView)中显示项目。我的UIbutton
低于textfield
。现在,当我从自动填充文本字段中选择一个项目并单击该按钮时,它没有响应。
我不知道为什么。
我已将其放在didendediting
和didSelectrow
中,但没有用。我已将UIbutton
添加到ScrollView
(testScroll)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Name.text = selectedCell.textLabel.text;
[Name resignFirstResponder];
[testScroll bringSubviewToFront:btnSearch];
autocompleteTableView.hidden=true;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
if(textField==CompanyName)
{
[testScroll bringSubviewToFront:btnSearch];
}
}
我哪里出错?
编辑:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if(textField==Name)
{
autocompleteTableView.hidden = NO;
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
return YES;
if([Name.text length]==0)
{
autocompleteTableView.hidden = YES;
}
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
parser = [[NSXMLParser alloc] initWithData:receivedData];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
[parser release];
if([arr4 count]!=0)
{
self.autocompleteUrls = [[[NSMutableArray alloc] init]autorelease];
viewForautoCompleteTableView = [[UIView alloc]initWithFrame:CGRectMake(220, 370, 295, 230)];
if(autocompleteTableView)
[autocompleteTableView removeFromSuperview];
autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,295,150) style:UITableViewStyleGrouped];
autocompleteTableView.delegate = self;
autocompleteTableView.dataSource = self;
autocompleteTableView.scrollEnabled = YES;
autocompleteTableView.backgroundColor = [UIColor lightTextColor];
autocompleteTableView.rowHeight=28;
autocompleteTableView.backgroundView = nil;
autocompleteTableView.backgroundColor = [UIColor whiteColor];
autocompleteTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[autocompleteTableView setSeparatorColor:[UIColor orangeColor]];
[viewForautoCompleteTableView setFrame:CGRectMake(220,370 ,295,autocompleteTableView.frame.size.height)];
[viewForautoCompleteTableView addSubview:autocompleteTableView];
[self.view addSubview:viewForautoCompleteTableView];
[autocompleteUrls removeAllObjects];
for(int i=0;i<[arr4 count];i++)
{
NSString *curString = [NSString stringWithFormat:@"%@ %@",[[arr4 objectAtIndex:i] valueForKey:@"Name"],[[arr4 objectAtIndex:i]valueForKey:@"LastName"]];
[autocompleteUrls addObject:curString];
}
}
[autocompleteTableView reloadData];
}
答案 0 :(得分:2)
我遇到了同样的问题,并在下面的代码的帮助下修复了。
编辑以下两个功能如下:
in .h:
int isValueSelected;
in .m:
在viewDidLoad中: isValueSelected = 0;
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (isValueSelected == 1)
{
autocompleteTableView.hidden = YES;
isValueSelected = 0;
}else{
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
[self searchAutocompleteEntriesWithSubstring:substring];
if((substring.length ==0) || ([substring characterAtIndex:0] == 10) || (autocompleteUrls.count == 0)){
autocompleteTableView.hidden = YES;
}else{
autocompleteTableView.hidden = NO;
}
}
return YES;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
urlField.text = selectedCell.textLabel.text;
isValueSelected = 1;
autocompleteTableView.hidden = YES;
}