如何使用此代码在tableview中使用搜索栏?

时间:2014-01-09 11:01:55

标签: ios objective-c uitableview uitextfield

如何使用cell.textLabel.text搜索textfield值?我正在使用此代码。

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];


cell.selectionStyle = UITableViewCellSelectionStyleNone;


UITextField* tf = nil ;
switch ( indexPath.row ) {
    case 0: {
        cell.textLabel.text = @"Name" ;
        tf = nameField_ = [self makeTextField:self.name placeholder:@"sathish"]; 
        [cell addSubview:nameField_];
        break ;
    }
    case 1: {
        cell.textLabel.text = @"Address" ;
        tf = addressField_ = [self makeTextField:self.address placeholder:@"example@gmail.com"];
        [cell addSubview:addressField_];
        break ;
    }
    case 2: {
        cell.textLabel.text = @"Password" ;
        tf = passwordField_ = [self makeTextField:self.password placeholder:@"Required"];
        [cell addSubview:passwordField_];
        break ;
    }
    case 3: {
        cell.textLabel.text = @"Description" ;
        tf = descriptionField_ = [self makeTextField:self.description placeholder:@"My Gmail Account"];
        [cell addSubview:descriptionField_];
        break ;
    }
}

tf.frame = CGRectMake(120, 12, 170, 30);    

[tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];    

tf.delegate = self ;


    return cell;
}

1 个答案:

答案 0 :(得分:1)

基本步骤:

  1. 在您的班级中声明并初始化UISearchBar
  2. 声明并初始化UISearchDisplayController
  3. 将searchBar添加到您的tableView
  4. 实现UISearchDisplayController委托方法
  5. 这个受欢迎的链接你会得到更多的想法

    Adding UISearchBar Programmatically to UITableView