iOS学习笔记12UISearchBar smallelephant_A

首先做一个假数据

    data=[NSArray arrayWithObjects:@"w",@"wwe",@"wwweer",@"Eee",@"fff",@"fffq", nil];

定义一个bool 值是isFind 初始值设为0

    isfind=NO;即不是搜索状态

 设置搜索状态和非搜索状态的不同的容行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (isfind==NO) {

        return data.count;

    }

    else

    {

        return finddata.count;

    }

}

设置cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellid=@"cellid";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];

    if (cell==nil) {

        cell=[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleDefault reuseIdentifier:cellid];

    }

    if (isfind==YES) {

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

    }

    return cell;

}

原文地址:https://www.cnblogs.com/adodo/p/5206893.html