如何在表格视图中添加搜索栏

问题描述:

实际上,我在表格视图中存储了太多数据,现在我想轻松找到我的数据,因此我需要一个搜索栏。任何人都可以给我一点解决方案吗?如何在表格视图中添加搜索栏

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifire = @"CellIdentifire"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire]; 
    } 
    cell.textLabel.text = [array objectAtIndex:indexPath.row]; 
    return cell; 
} 
+1

感谢所有,但我发现我的答案,如果你需要请尝试“URL” http://www.appcoda.com/how-to-add-search-bar-uitableview/ – 2014-10-28 15:22:54

遵循以下步骤:

  • 拖放
  • 搜索栏
  • 集委托上表视图头下降搜索栏视图控制器

使用下面的代码搜索:

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
    [searchBar setShowsCancelButton:YES]; 
} 

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 

    //Remove all objects first. 
    [self searchTerm:searchText]; 
} 

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"Cancel clicked"); 
    searchBar.text = @""; 
    [searchBar resignFirstResponder]; 
    [searchBar setShowsCancelButton:NO]; 

    [self showAllData];// to show all data 
} 

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    NSLog(@"Search Clicked"); 
    [searchBar setShowsCancelButton:NO]; 
    [searchBar resignFirstResponder]; 

    [self searchTerm:searchBar.text]; 
} 

-(void)showAllData { 
    //load all data in _tableViewDataSourceArray, and reload table 
} 

-(void)searchTerm : (NSString*)searchText 
{ 
    if (searchText == nil) return; 

    _tableViewDataSourceArray = //YOUR SEARCH LOGIC   
    [self.tableView reloadData]; 
} 

编辑: 如果你想创建代码搜索栏,然后初始化一个搜索栏,并把它作为headerView你的表视图,里面

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

不要的流连忘返的景色高度

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
+0

我需要所有的工作编程,你能帮我吗。 – 2014-10-28 15:12:29

+0

@sumitsingh检查编辑。 – rptwsthi 2014-10-28 15:25:45

+0

好的,谢谢bro其工作“+1” – 2014-10-28 16:01:53