Jea*_*ard 12 iphone uitableview uisearchbar ios
我有一个NSMutableArray显示UITableView,我在那里添加了一些元素.
例如,元素名称是First,FirstTwo,Second,SecondTwo,Third,ThirdTwo.
现在我想在屏幕上添加一个搜索栏.在我键入F的那个搜索栏中,表格应该只显示First和FirstTwo.
我该怎么做?
Sac*_*hin 27
 searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
 searchBar.delegate = self;
 self.tableView.tableHeaderView = searchBar;
Joe*_*jah 17
获得这方面的最好方法是在这里 通过这里的教程.您正在寻找的部分是:
- (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];
}
| 归档时间: | 
 | 
| 查看次数: | 32600 次 | 
| 最近记录: |