文本字段为空时隐藏自动完成表格视图

时间:2018-03-16 07:06:36

标签: objective-c uitableview uitextfield

以下代码将在文本字段autocompleteTableView下方生成自定义自动填充表格视图txtActivity。当文本字段为空时,如何隐藏表视图。

- (void)viewDidLoad
{    
     [super viewDidLoad];
     self.txtActivity.delegate = self;
     [self cofigureAutoComTableView];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString *)string 
{
    autocompleteTableView.hidden = NO;

    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
    return YES;
}

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

    autocompleteMArray = [[NSMutableArray alloc] init];

    [autocompleteMArray removeAllObjects];

    for(NSString *curString in sCategory) { //**** sCategory is a mutable array generated in another method not shown ****

    NSRange substringRange = [curString rangeOfString:substring];

    if (substringRange.length > 0) {

        [autocompleteMArray addObject:curString];
    }

}
  NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
  [autocompleteTableView reloadData];
}


-(void)cofigureAutoComTableView
{
     autocompleteTableView = [[UITableView alloc] initWithFrame:CGRectMake(self.txtActivity.frame.origin.x-10,self.txtActivity.frame.origin.y+32,self.txtActivity.frame.size.width, 120) style:UITableViewStylePlain];

     autocompleteTableView.delegate = self;
     autocompleteTableView.dataSource = self; //**** Setting this to self, is it correct???
     autocompleteTableView.scrollEnabled = YES;
     autocompleteTableView.hidden = YES;
     [self.view addSubview:autocompleteTableView];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return autocompleteMArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cellIdentifier";

    UITableViewCell *cell = [self.autocompleteTableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if(cell == nil) 
    {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }

     cell.textLabel.text =  [autocompleteMArray objectAtIndex:indexPath.row];

    return cell;
  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath (NSIndexPath *)indexPath;
{
    //**** NOT DETECTING *******
    NSLog(@"Selected Cell %@", [autocompleteMArray objectAtIndex:indexPath.row]);
}

2 个答案:

答案 0 :(得分:0)

如果substringRange长度为0,则将sCategory数组分配给autocompleteMArray并重新加载表格视图。

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
 {
    autocompleteMArray = [[NSMutableArray alloc] init];
    [autocompleteMArray removeAllObjects];
    if ([substring length] == 0)
    {
      for(NSString *curString in sCategory)
      { //**** sCategory is a mutable array generated in another method not shown ****
        NSRange substringRange = [curString rangeOfString:substring];
        if (substringRange.length > 0)
        {
          [autocompleteMArray addObject:curString];
        }
      }
    }
    else
    {
      autocompleteMArray = sCategory;
    }
    NSLog(@"*******The autocompleteMArray : %@ ", autocompleteMArray);
    [autocompleteTableView reloadData];
 }

答案 1 :(得分:0)

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([[NSString stringWithFormat:@"%@%@",textField.text,string] isEqualToString:@""]) {
    autocompleteTableView.hidden = YES;
} else {
    autocompleteTableView.hidden = range.location == 0 ? YES : NO;
    NSString *substring = [NSString stringWithString:textField.text];
    substring = [substring stringByReplacingCharactersInRange:range withString:string];
    [self searchAutocompleteEntriesWithSubstring:substring];
}
return YES; }