2012-11-16 37 views
1

我對Objective-C相當陌生,併爲UItextField實現自動完成功能的概念。我可以適當地做到這一點。但是當我選擇特定的單元格時,那麼該單元格的文本應該顯示在UITextField中,並且相應地tableView必須隱藏。但我無法隱藏UITableView後選擇一個單元格..在哪裏我錯了?TableView沒有隱藏在xcode的didSelectRowAtIndexPath中?

- (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]; 
    [email protected]"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; 

    } 

如何在選擇行後隱藏autocompleteTableView? 任何幫助,將不勝感激..

+0

的這一翻譯這個'autocompleteTableView.hidden = YES;''嘗試tableView.hidden = YES;' –

+0

走了it.still沒有得到 – Honey

+0

我認爲你在其他地方有問題。 ....'autocompleteTableView.hidden = YES;''和'tableView.hidden = YES;'正在工作正常 – Venkat

回答

0

問題是如果條件中,當if被評估爲true,則再次autocompleteTableView重新分配,並添加到self.view 。它將被放置在前一個tableView上,並且你將丟失之前tableView的引用。如果您致電autocompleteTableView.hidden = YES。最後加入tableView將被隱藏。但以前添加tableViews將在那裏。

只要改變,如果像塊:

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]; 
} 
+0

但現在當我選擇一個單元格然後tableview消失,但選定單元格文本不顯示在UITextField.-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {沒有得到調用...我應該修改什麼ELZ? – Honey

+0

@arizah:' - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath'正在工作,否則它不會隱藏tableView。 –

+0

k瞭解它..謝謝很多。我一直在努力爲此付出努力。謝謝你再次感謝 – Honey

-2

做了嘗試:

[self.tableView setHidden:YES]; 
+0

是的,但我沒有運氣 – Honey

+0

@simone聽到兩個答案一樣,因爲你沒有新的代碼...在你的答案 –

+0

@nitin tableView si不同於self.tableView如果你有財產 –

0

@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];