2014-10-28 280 views
0

實際上,我在表格視圖中存儲了太多數據,現在我想輕鬆找到我的數據,因此我需要一個搜索欄。任何人都可以給我一點解決方案嗎?如何在表格視圖中添加搜索欄

- (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

回答

1

遵循以下步驟:

  • 拖放
  • 搜索欄
  • 集委託上表視圖頭下降搜索欄視圖控制器

使用下面的代碼搜索:

- (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

相關問題