繁体   English   中英

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

[英]Implementing Search Bar on UITableView Objective C - iOS 9 or later

我创建了一个应用程序,在其中我将google firebase用于后端,将Objective-C用于前端。

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

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

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

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

谢谢堆

虽然您正在谈论搜索栏。 但根据您的要求,最好使用Search Controller而不是独立使用搜索栏。 因为您会发现很多委托方法,所以您不必费力就可以了。

此代码用于过滤器数组。

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

在.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];

                }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM