Table View SearchBar


App link:  https://drive.google.com/open?id=0BwU5Y4Xqfa42UllCZUpsUF9xdXM



appdelegate.h


#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;



@end

appdelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

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

@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

@property UITableView*tv;
@end

viewController.m
#import "ViewController.h"
#import "TableViewCell.h"

@interface ViewController (){
    NSMutableURLRequest *urlReq;
    NSURLSession *urlSes;
    NSURLSessionConfiguration *urlSesConfig;
    NSURLSessionDataTask *urlDataTask;
    NSMutableDictionary *dist;
    NSMutableArray *projectNameArr;
    NSMutableArray *projectImageArr;
    NSMutableArray *projectCountryArr;
    NSMutableArray *projectYearArr;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    projectNameArr=[[NSMutableArray alloc]init];
    projectImageArr=[[NSMutableArray alloc]init];
    projectCountryArr=[[NSMutableArray alloc]init];
    projectYearArr=[[NSMutableArray alloc]init];
    
    self.tv=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tv];
    self.tv.delegate=self;
    self.tv.dataSource=self;
    
    
    urlReq =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://qa.mobtecnica.com/irise/web/index.php?r=api/projectsearch"]];
    
    
    urlSesConfig =[NSURLSessionConfiguration defaultSessionConfiguration];
    urlSes =[NSURLSession sessionWithConfiguration:urlSesConfig];
    
    [urlReq setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [urlReq setHTTPMethod:@"POST"];
    
   NSString * post = [[NSString alloc] initWithFormat:@"id=%@&session_id=%@&keyword=%@&country=%@&city=%@&products=%@&architect=%@&collection=%@&building=%@&objects=%@",@"235",@"kqMh3AAenD",@"",@"",@"",@"",@"",@"",@"",@""];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    
    [urlReq setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlReq setHTTPBody:postData];
    
    
    
    urlDataTask=[urlSes dataTaskWithRequest:urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                 
                 {
                     
                     
                         dist=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                         NSLog(@"values are %@",dist);
                     
                     for (int i=0; i<7; i++) {
                         NSString*str=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"name"];
                         [projectNameArr addObject:str];
                     }
                     NSLog(@"values of project names are %@",projectNameArr);
                     
                     for (int i=0; i<7; i++) {
                         
                         NSString*str1=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"country"];
                         [projectCountryArr addObject:str1];
                         
                     }
                     NSLog(@"values of project country are %@",projectCountryArr);
                     for (int i=0; i<7; i++) {
                         
                         NSString*str2=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"completion_year"];
                         [projectYearArr addObject:str2];
                         
                         
                     }
                     NSLog(@"values of project year are %@",projectYearArr);
                     
                     //                        for (int i=0; i<7; i++) {
                     //                         NSString*str3=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"image"];
                     //
                     //                         NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:str3]];
                     //
                     //                         [projectImageArr addObject:[UIImage imageWithData:imageData]];
                     //
                     //                         
                     //                     }
                     //NSLog(@"values of project year are %@",projectImageArr);
                     
                     
                 }];
    [self.tv reloadData];
    [urlDataTask resume];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return projectNameArr.count;;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}


//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reUse" forIndexPath:indexPath];
//    NSString*reuseId=@"reUse";
//    TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:reuseId];
//    if (cell==nil) {
//        NSArray*cellArr=[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];
//        cell=[cellArr objectAtIndex:0];
//        cell.projectNameLbl.text=[projectNameArr objectAtIndex:indexPath.row];
//        //cell.projectImg.image=[projectImageArr objectAtIndex:indexPath.row];
//        cell.projectCountryLbl.text=[projectCountryArr objectAtIndex:indexPath.row];
//        cell.projectYearLbl.text=[projectYearArr objectAtIndex:indexPath.row];
//        [self.tv reloadData];
//    }
//    
//    
//    return cell;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifier=@"id";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@",[projectNameArr objectAtIndex:indexPath.row]];
    //  cell.imageView.image =[self.imageArray objectAtIndex:indexPath.row];
    
    
    return cell;
}



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


@end

TableVC.h

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController<UISearchResultsUpdating>
@property UISearchController*sc;


@end

TableVC.m


#import "TableViewController.h"
#import "TableViewCell.h"

@interface TableViewController (){
    NSMutableURLRequest *urlReq;
    NSURLSession *urlSes;
    NSURLSessionConfiguration *urlSesConfig;
    NSURLSessionDataTask *urlDataTask;
    NSMutableDictionary *dist;
    NSMutableArray *projectNameArr;
    NSMutableArray *projectImageArr;
    NSMutableArray *projectCountryArr;
    NSMutableArray *projectYearArr;
    NSArray*filterArr;
     NSMutableDictionary *imgDic;
     NSArray *filterImg;
    NSMutableDictionary *pNameDict;
   // NSArray *filterName;
    NSMutableDictionary*pYearDict;
}

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 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;
    projectNameArr=[[NSMutableArray alloc]init];
    projectImageArr=[[NSMutableArray alloc]init];
    projectCountryArr=[[NSMutableArray alloc]init];
    projectYearArr=[[NSMutableArray alloc]init];
    imgDic=[[NSMutableDictionary alloc]init];
    pNameDict=[[NSMutableDictionary alloc]initWithObjects:projectNameArr forKeys:projectCountryArr];
    
    self.sc=[[UISearchController alloc]initWithSearchResultsController:nil];
    self.tableView.tableHeaderView=self.sc.searchBar;
    self.sc.searchResultsUpdater=self;
    self.definesPresentationContext = YES;
    self.sc.dimsBackgroundDuringPresentation = NO;
    
    self.sc.searchBar.placeholder=@"Search Celebrity";
    self.sc.searchBar.showsBookmarkButton=YES;

    
    
    
    
    urlReq =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://qa.mobtecnica.com/irise/web/index.php?r=api/projectsearch"]];
    
    
    urlSesConfig =[NSURLSessionConfiguration defaultSessionConfiguration];
    urlSes =[NSURLSession sessionWithConfiguration:urlSesConfig];
    
    [urlReq setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [urlReq setHTTPMethod:@"POST"];
    
    NSString * post = [[NSString alloc] initWithFormat:@"id=%@&session_id=%@&keyword=%@&country=%@&city=%@&products=%@&architect=%@&collection=%@&building=%@&objects=%@",@"235",@"kqMh3AAenD",@"",@"",@"",@"",@"",@"",@"",@""];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    
    [urlReq setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [urlReq setHTTPBody:postData];
    
    
    
    urlDataTask=[urlSes dataTaskWithRequest:urlReq completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
                 
                 {
                     
                     
                     dist=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                     NSLog(@"values are %@",dist);
                     
                     for (int i=0; i<7; i++) {
                         NSString*str=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"name"];
                         [projectNameArr addObject:str];
                      //   [pNameDict setObject:str forKey:[projectCountryArr objectAtIndex:i]];
                    }
                     NSLog(@"values of project names are %@",projectNameArr);

                     for (int i=0; i<7; i++) {
                     
                         NSString*str1=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"country"];
                         [projectCountryArr addObject:str1];
                         
                     }
                     NSLog(@"values of project country are %@",projectCountryArr);
                     for (int i=0; i<7; i++) {
                     
                         NSString*str2=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"completion_year"];
                         [projectYearArr addObject:str2];
                         
                         
                     }
                     NSLog(@"values of project year are %@",projectYearArr);
                         
                        for (int i=0; i<7; i++) {
                         NSString*str3=[[[dist objectForKey:@"data"]objectAtIndex:i]objectForKey:@"image"];
                         
                         NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:str3]];
                         
                         [projectImageArr addObject:[UIImage imageWithData:imageData]];
                         
                         [imgDic setObject:[UIImage imageWithData:imageData] forKey:[projectCountryArr objectAtIndex:i]];
                     }
NSLog(@"values of project year are %@",projectImageArr);
                     
        pNameDict=[[NSMutableDictionary alloc]initWithObjects:projectNameArr forKeys:projectCountryArr];
                     pYearDict=[[NSMutableDictionary alloc]initWithObjects:projectYearArr forKeys:projectCountryArr];
                     
                     [self.tableView reloadData];
                 }];
    
    
    

    [urlDataTask resume];
   
    

}

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

#pragma mark - Table view data source

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    
    
    NSLog(@"typed text for searching is %@",searchController.searchBar.text);
    NSPredicate*predicate=[NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchController.searchBar.text];
    
    [projectCountryArr filteredArrayUsingPredicate:predicate];
    
    filterArr=[projectCountryArr filteredArrayUsingPredicate:predicate];
    filterImg=[[imgDic objectForKey:projectCountryArr] filteredArrayUsingPredicate:predicate];
 // filterName=[[pNameDict objectForKey:projectCountryArr]filteredArrayUsingPredicate:predicate];
   // NSLog(@"filter image is %@",filterImg);
    [self.tableView reloadData];

    
   // [self.tableView reloadData];
    
}
//





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    NSLog(@"table view values are %lu",projectNameArr.count);
    
    if (self.sc.active==YES) {
        return filterArr.count;
    }
    else{
    return projectNameArr.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300;
}


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

    
    TableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"re"];
    NSArray*cellArr=[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil];
    cell=[cellArr objectAtIndex:0];
    if (self.sc.active==YES) {
        cell.projectCountryLbl.text=[filterArr objectAtIndex:indexPath.row];
        cell.projectImg.image=[imgDic valueForKey:[filterArr objectAtIndex:indexPath.row]];
        
        NSString*indexString=[filterArr objectAtIndex:indexPath.row];
       // wv.webViewStr=[self.webDictionary valueForKey:indexString];
        cell.projectNameLbl.text=[pNameDict objectForKey:indexString];
        NSString*indexString1=[filterArr objectAtIndex:indexPath.row];
        // wv.webViewStr=[self.webDictionary valueForKey:indexString];
        cell.projectYearLbl.text=[pYearDict objectForKey:indexString1];

    }
    else{
    
        cell.projectNameLbl.text=[projectNameArr objectAtIndex:indexPath.row];
        //cell.projectYearLbl.text=[projectYearArr objectAtIndex:indexPath.row];
        cell.projectYearLbl.text=[NSString stringWithFormat:@"%@",[projectYearArr objectAtIndex:indexPath.row]];
       cell.projectImg.image=[projectImageArr objectAtIndex:indexPath.row];
       cell.projectCountryLbl.text=[projectCountryArr objectAtIndex:indexPath.row];
    }

    
   
    return cell;
}


//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSString *identifier=@"id";
//    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//    if (cell==nil) {
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//        
//    }
//    
//    cell.textLabel.text=[NSString stringWithFormat:@"%@",[projectNameArr objectAtIndex:indexPath.row]];
//  //  cell.imageView.image =[self.imageArray 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.
    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 deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } 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
    }   
}
*/

/*
// 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.
}
*/


@end

TableViewCell.h

#import <UIKit/UIKit.h>

@interface TableViewCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *projectNameLbl;
@property (strong, nonatomic) IBOutlet UILabel *projectCountryLbl;
@property (strong, nonatomic) IBOutlet UILabel *projectYearLbl;
@property (strong, nonatomic) IBOutlet UIImageView *projectImg;

@end


TableViewCell.m


#import "TableViewCell.h"

@implementation TableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end


Previous
Next Post »