how to add searchbar in uitableview?

前端 未结 4 876
轻奢々
轻奢々 2020-12-28 19:48

I have an NSMutableArray displayed in a UITableView, and I have added some elements there.

For example, element names are First, FirstTwo,

相关标签:
4条回答
  • 2020-12-28 20:21

    The best way to get the hang of this is by following a tutorial over here over here. The part you are looking for, is this:

    - (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];
    }
    
    0 讨论(0)
  • 2020-12-28 20:34

    If you are using search bar. then You can search your contain of table view using delegate method of searchbar

    (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
    // myArray is main array for Table View
    // tempMainArray which store myArray Data
    
    myArray = [[NSMutableArray alloc]initWithArray:tempMainArray];
    NSMutableArray *searchArray = [[NSMutableArray alloc]init];
    
    
    [searchArray removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""]|| searchText==nil){
    
        [myArray removeAllObjects];
        myArray =  tempMainArray;
        [_objTableView reloadData];
    
        return;
    }
    
    NSInteger counter = 0;
    for(NSString *name in myArray)
    {
    
        NSRange r = [name rangeOfString:searchText];
        if(r.location != NSNotFound)
        {
            if(r.location== 0)//that is we are checking only the start of the names dynamicaly.
            {
                [searchArray addObject:name];
            }
        }
    
        counter++;
    
    
    }
    
    if (searchArray.count > 0) {
    
        [myArray removeAllObjects];
         myArray =  searchArray;
        [_objTableView reloadData];
    }
    else
    {
        [myArray removeAllObjects];
        myArray =  tempMainArray;
        [_objTableView reloadData];
    }
    
    }
    
    0 讨论(0)
  • 2020-12-28 20:38

    I did it myself and it is working absolutely fine.

    Declare two mutable arrays. One containing all the data and second for storing filtered data. Here searchuser is search bar outlet. aray is main array storing all data. set searchbar capitalisation property to sentence so that this would work correctly.

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
        [searchuser.text lowercaseString]);
        filteredarray=[[NSMutableArray alloc]init];
        int l=(int)[searchuser.text length];
        if (l>0)
        {
            NSMutableString * data=[[NSMutableString alloc]init];
            int a=(int)[aray count];
    
            for (int i=0;i<=a-1;i++)
            {
                data=[aray objectAtIndex:i];
                if ([searchuser.text isEqualToString:data])
                {
                    [filteredarray addObject:data];
                }
                if ([data length]>l)
                {
                    NSMutableString * string=[[NSMutableString alloc]initWithString:[data substringWithRange:NSMakeRange(0, l)]];
    
                    if ([searchuser.text isEqualToString:string])
                    {
                        [filteredarray addObject:data];
    
                    }
                }
            }
        }
        else
        {
            filteredarray=aray;
        }
        [tableview reloadData];
    }
    
    0 讨论(0)
  • 2020-12-28 20:39
     searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    
     searchBar.delegate = self;
    
     self.tableView.tableHeaderView = searchBar;
    
    0 讨论(0)
提交回复
热议问题