Search in Table view Controller







tableVC.h
#import <UIKit/UIKit.h>

@interface customTableViewController : UITableViewController<UISearchBarDelegate,UISearchResultsUpdating>
@property NSMutableArray *display,*actors;
@property NSArray *filter1,*filter2;
@property UISearchController *SC;


@end

tableVC.m

#import "customTableViewController.h"

@interface customTableViewController ()

@end

@implementation customTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _display=[[NSMutableArray alloc]initWithObjects:@"mahesh",@"pavan",@"kishore",@"naresh",@"harish",@"ram",@"sita",@"apsar",@"akshay",@"mahesh palla",@"swamy",@"manikanta",@"dinesh",@"goud", nil];
    _actors=[[NSMutableArray alloc]initWithObjects:@"nagarjuna",@"venky",@"mahesh",@"balaya",@"rana",@"suresh",@"ntr",@"hari",@"uday",@"nitin",@"kalyan", nil];
    _SC=[[UISearchController alloc]initWithSearchResultsController:nil];
    _SC.searchBar.delegate=self;
    _SC.searchResultsUpdater=self;
     self.tableView.tableHeaderView = _SC.searchBar;
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.SC.active==YES) {
        if (section==0) {
            return [_filter1 count];
        }
        else{
            return [_filter2 count];
        }
    }
    
    if(section==0){
        return [_display count];
    }else{
        return [_actors count];
    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CTV" forIndexPath:indexPath];
    
    if (self.SC.active==YES) {
        if (indexPath.section==0) {
            cell.textLabel.text=[_filter1 objectAtIndex:indexPath.row];
        }
        else{
            cell.textLabel.text=[_filter2 objectAtIndex:indexPath.row];
        }
      
    } else {
        if (indexPath.section==0) {
            cell.textLabel.text=[_display objectAtIndex:indexPath.row];
        }
        else{
            cell.textLabel.text=[_actors objectAtIndex:indexPath.row];
        }
        
    }
    
    return cell;
}



// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    if (indexPath.row==0 || indexPath.row==3 ||indexPath.row==6) {
        return NO;
    }
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView beginUpdates];
        [_display removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        [tableView beginUpdates];
       
        NSString *name=@"kishore";
        [_display insertObject:name atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView endUpdates];
        
    }   
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row==0 || indexPath.row==3 ||indexPath.row==5 || indexPath.row==7) {
        return UITableViewCellEditingStyleInsert;
    } else{
        return UITableViewCellEditingStyleDelete;
    }
}


// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}



// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
    NSPredicate *predicate=[NSPredicate  predicateWithFormat:@"SELF BEGINSWITH [c] %@",searchController.searchBar.text];
    _filter1=[_display filteredArrayUsingPredicate:predicate];
    _filter2=[_actors filteredArrayUsingPredicate:predicate];
    [self.tableView reloadData];
}
- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return @[@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",@"U",@"V",@"W",@"X",@"Y",@"Z"];
}
@end
Previous
Next Post »