TableView 不会隐藏在 xcode 中的 didSelectRowAtIndexPath 中



我对objective-C相当陌生,并为UItextField实现了自动完成功能的概念.我可以适当地做到这一点.但是当我选择一个特定的单元格时,该单元格的文本应该显示在UITextField中,相应地tableView必须被隐藏。但是我无法在选择单元格后隐藏UITableView。我哪里出错了?

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring {

    NSURL *urlString= [NSString stringWithFormat:@"http://179.87.89.90/services/Service.svc/GetCities/?p=%@&k=%@",substring,txtId.text];     
    NSURL *jsonUrl =[NSURL URLWithString:urlString];
    NSString *jsonStr = [[NSString alloc] initWithContentsOfURL:jsonUrl];  
    parser = [[NSXMLParser alloc] initWithContentsOfURL:jsonUrl];
    currentHTMLElement=@"3";
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    [parser release];
    NSLog(@"%@",arr2);

    if([arr2 count]!=0)
    {
        self.autocompleteUrls = [[NSMutableArray alloc] init];
        autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(88, 447, 200, 120) style:UITableViewStyleGrouped];
        autocompleteTableView.delegate = self;
        autocompleteTableView.dataSource = self;
        autocompleteTableView.scrollEnabled = YES;
        // autocompleteTableView.hidden = YES;  
        [self.view addSubview:autocompleteTableView];
        [autocompleteUrls removeAllObjects];
            for(int i=0;i<[arr2 count];i++)
            {
                NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"];
                NSRange substringRange = [curString rangeOfString:substring];
                if (substringRange.location == 0) 
                    [autocompleteUrls addObject:curString];  
            }
      [autocompleteTableView reloadData];
    }
    else
    {
        autocompleteTableView.delegate=nil;
        autocompleteTableView.dataSource=nil;
        autocompleteTableView.hidden = YES;  
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if( textField == txtcity)
    {
     autocompleteTableView.hidden = NO;
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) section {
    return autocompleteUrls.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = nil;
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier";
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease];
    }
    cell.textLabel.text = [autocompleteUrls objectAtIndex:indexPath.row];
    cell.textLabel.font=[UIFont boldSystemFontOfSize:12];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
    txtcity.text = selectedCell.textLabel.text;
    [autocompleteUrls removeAllObjects];
    [self.autocompleteTableView setHidden:YES];    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
        txtcity.text = selectedCell.textLabel.text;
        [autocompleteUrls removeAllObjects];
        autocompleteTableView.hidden=YES;
    }

选择一行后如何隐藏自动完成表视图?任何帮助将不胜感激。

问题出在 if 条件上,当if被评估为 true 时,autocompleteTableView再次被分配并添加到 self.view 中。它将放置在上一个表视图上,并且您将丢失上一个表视图的引用。如果你打电话给autocompleteTableView.hidden = YES.最后添加的tableView将被隐藏。但是以前添加的表视图将在那里。

只需更改 if 块,例如:

if([arr2 count]!=0)
{
    self.autocompleteUrls = [[NSMutableArray alloc] init];
    if(autocompleteTableView)
         [autocompleteTableView removeFromSuperView];
    autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(88, 447, 200, 120) style:UITableViewStyleGrouped];
    autocompleteTableView.delegate = self;
    autocompleteTableView.dataSource = self;
    autocompleteTableView.scrollEnabled = YES;
    // autocompleteTableView.hidden = YES;  
    [self.view addSubview:autocompleteTableView];
        for(int i=0;i<[arr2 count];i++)
        {
            NSString *curString = [[arr2 objectAtIndex:i] valueForKey:@"Name"];
            NSRange substringRange = [curString rangeOfString:substring];
            if (substringRange.location == 0) 
                [autocompleteUrls addObject:curString];  
        }
  [autocompleteTableView reloadData];
}

@Arizah - 请尝试创建一个全新的应用程序来测试 UItableview 和 UItextField 委托方法。可能是某个地方——

autocompleteTableView.delegate=nil;
autocompleteTableView.dataSource=nil;

被调用,因此进一步没有委托方法,如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

不会被召唤。尝试避免使用此代码。

还有声明: self.autocompleteUrls = [[NSMutableArray alloc] init]; 在技术上是不正确的,因为保留属性不需要分配。相反,您可以使用:

NSMutableArray *theArray= [[NSMutableArray alloc] init];
self.autocompleteUrls = theArray;
[theArray release];

你试过吗:

[self.tableView setHidden:YES];

最新更新