根据用户动态显示tableview上的数据

时间:2014-12-17 06:08:15

标签: ios objective-c uitableview

如果用户在文本字段中输入,则表格视图将根据用户输入显示并显示数据,例如,如果用户插入A,则从表格视图中显示所有单词 为此我在用户选择文本字段时显示了tableview,但我显示了所有数据列表,如下拉选项,但客户端希望它会自动更正

这是我的代码

 in textfielddidbeginediting:---

    if (textField ==_stateTextField)
{
    _stateView.hidden = NO;
    _cityView.hidden  = YES;
    [self.view endEditing:YES];
    [_tableView reloadData];

}

 **in tableview method** 
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
    return [_stateListArray count];
  }

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
StateListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell)
{
    [tableView registerNib:[UINib nibWithNibName:@"StateListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
}
cell.listLabel.text = [_stateListArray objectAtIndex:indexPath.row];
return cell;
}
 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {
_stateName = [_stateListArray objectAtIndex:indexPath.row];
_stateTextField.text =_stateName;
_stateView.hidden = YES;
 }

2 个答案:

答案 0 :(得分:2)

您可以使用

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string {
//Your Code
[tableView reloadData];
  }

委托方法,然后重新加载表。

希望能帮到你。

答案 1 :(得分:1)

在shouldChangeCharactersInRange中调用您的函数,因为此方法每次在文本字段中输入时都会调用: -

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range   replacementString:(NSString *)string {
     NSLog(@"%@",textField.text);
     // Your function here
     return YES;
    }