iosobjective-cuitableviewuisearchbar

How to make different search field for every sections in UITableview?


Suppose I want to implement UITableview with search field for each section. If I search in first sections resulted rows should be updated in first sections only. Same way if i search in second section, Second section resulted rows should be update and so on for rest of the sections.

I had implement with each section search field but it reloads section when i am reloading section or reload rows for section.


Solution

  • I found the solution, please find below code for it. As i am using AutoLayout & Storyboard for my view controller I took IBOutlets and as of now I have only 2 sections so I declared as static, but you can make this dynamic by creating views if you have more sections.

    // define in your .h file and take layout for the views and make it saperate from your main view container (outside of main view controller).
     
    @property(strong,nonatomic)IBOutlet UITextField * txtSearchSection1;
    @property(strong,nonatomic)IBOutlet UITextField * txtSearchSection2;
    
    @property (strong, nonatomic) IBOutlet UIView *viewSection1;
    @property (strong, nonatomic) IBOutlet UIView *viewSection2;
    
    
    // Implement in your .m file 
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 2;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return 40.0f;
    }
    -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        if (section==1) {
            return _viewSection1;
        }else{
            return _viewSection2;
        }
    }