在UITableView上实现搜索栏目标C - iOS 9或更高版本

时间:2017-05-17 03:07:49

标签: ios objective-c uitableview firebase

我创建了一个应用程序,其中我使用google firebase作为后端,而Objective-C作为前端。

它成功地在UITableView上存储和显示数据。

现在,我正在尝试在表格视图上方添加搜索栏,并尝试了许多来自互联网的示例,但其中大多数都已过时。

有谁可以帮助我,如何在iOS 9或更高版本的 Objective-C 中的表格视图上方添加搜索栏?

另外,当我们在搜索栏中输入时如何过滤数据?

谢谢堆

3 个答案:

答案 0 :(得分:0)

虽然你在谈论搜索栏。但根据您的要求,使用搜索控制器比独立搜索栏更好。因为有许多代表方法,你会发现这些。所以你不必为了额外的努力而头痛。

答案 1 :(得分:0)

此代码用于过滤器数组。

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text
{
     NSArray *resultarray;

     NSString *strSearch = [NSString stringWithFormat:@"%@",searchB.text];

      if ([strSearch isEqualToString:@""])
      {
         [self showAllData];

         return;
      }

     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self CONTAINS[cd] %@",strSearch];

     resultarray = [self.arrayALLData filteredArrayUsingPredicate:predicate];

     [self.arrayPeopleList removeAllObjects];

     [self.arrayPeopleList addObjectsFromArray:resultarray];

     [_tblView reloadData];

     return YES;
}

答案 2 :(得分:0)

在.h文件中声明:

    BOOL isFiltered;
    NSMutableArray *arrsearchresult ,*arrsearchname;
    NSMutableArray *arrfullname;
@property (strong, nonatomic) IBOutlet UITableView *tblAppoinment;

在.m文件中声明:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
                {
                    // Return the number of rows in the section.
                    /*if (tableView == self.searchDisplayController.searchResultsTableView) {
                     return [arrsearchresult count];

                     } else {
                     return [arrfullname count];
                     }*/

                    if(isFiltered)
                    {
                        return [arrsearchresult count];
                    }

                    return [arrfullname count];
                }


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


static NSString *simpleTableIdentifier = @"SimpleTableCell";

    AppoinmentTableViewCell *cell = (AppoinmentTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AppoinmentTableViewCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        if(isFiltered)
            {
                cell.lblFullname.text = [arrsearchresult objectAtIndex:indexPath.row];

            }




    }

    if(isFiltered)
     {
      cell.lblFullname.text = [arrsearchname objectAtIndex:indexPath.row];


     }
   }




                - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
                    isFiltered = YES;
                }



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



                            if(searchText.length==0)
                            {
                                isFiltered=NO;
                            }

                            else
                            {
                                isFiltered=YES;
                                arrsearchresult=[[NSMutableArray alloc]init];
                                arrsearchdate=[[NSMutableArray alloc]init];
                                arrsearchname=[[NSMutableArray alloc]init];
                                for ( NSString * str in arrfullname)
                                {
                                    NSRange stringRange = [str rangeOfString:searchText options:NSCaseInsensitiveSearch];

                                    NSInteger index;
                                    if(stringRange.location != NSNotFound)
                                    {
                                        [arrsearchresult addObject:str];
                                        index = [arrfullname indexOfObject:str];
                                        [arrsearchdate addObject:[arrleadCreatedDate objectAtIndex:index]];
                                        [arrsearchname addObject:str];
                                    }
                                }
                            }



                    [self.tblAppoinment reloadData];
                }

                -(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
                    [self.tblAppoinment resignFirstResponder];
                }

                - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
                    isFiltered=NO;
                    [self.tblAppoinment reloadData];

                }