如何在uitableview中添加搜索栏? [英] how to add searchbar in uitableview?

查看:124
本文介绍了如何在uitableview中添加搜索栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableView 中显示了 NSMutableArray ,我在那里添加了一些元素。

I have an NSMutableArray displayed in a UITableView, and I have added some elements there.

例如,元素名称是First,FirstTwo,Second,SecondTwo,Third,ThirdTwo。

For example, element names are First, FirstTwo, Second, SecondTwo, Third, ThirdTwo.

现在我要添加一个屏幕中的搜索栏。在我输入F的那个搜索栏中,表格应该只显示First和FirstTwo。

Now I want to add a search bar in the screen. In that search bar when I type F, the table should only show First and FirstTwo.

我应该怎么做?

推荐答案

最好的办法是遵循教程 在这里 在这里
您正在寻找的部分是:

The best way to get the hang of this is by following a tutorial over here over here. The part you are looking for, is this:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
   [tableData removeAllObjects];// remove all data that belongs to previous search
   if([searchText isEqualToString:@""]searchText==nil){
      [myTableView reloadData];
      return;
   }

   NSInteger counter = 0;
   for(NSString *name in dataSource)
   {
      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
      NSRange r = [name rangeOfString:searchText];
      if(r.location != NSNotFound)
      {
         if(r.location== 0)//that is we are checking only the start of the names.
         {
            [tableData addObject:name];
         }
      }

      counter++;
      [pool release];

   }

   [myTableView reloadData];
}

这篇关于如何在uitableview中添加搜索栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆