searchvc.h:
#import <UIKit/UIKit.h>
@interface SearchViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *search_TableView;
@property NSMutableDictionary *findImage_Dictionary,*findCategory_Dictionary;
@property NSString *nameStr;
//search
@property UISearchController*sc;
@end
searchvc.m:
#import "SearchViewController.h"
#import "SearchTableViewCell.h"
#import "DetailViewController.h"
@interface SearchViewController (){
NSArray *name_Array,*category_Array,*image_Array;
//search
NSArray *searchedName_Array;
}
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
name_Array = [[NSArray alloc]initWithObjects:@"Mahesh",@"Sukumar",@"Brami",@"Sathi",@"Arnab", nil];
category_Array=[[NSArray alloc]initWithObjects:@"Hero",@"Director",@"Comedy",@"TV",@"Anchor", nil];
image_Array=[[NSArray alloc]initWithObjects:[UIImage imageNamed:@"download.jpg"],[UIImage imageNamed:@"download-1.jpg"],[UIImage imageNamed:@"download-2.jpg"],[UIImage imageNamed:@"download-3.jpg"],[UIImage imageNamed:@"download-4.jpg"], nil];
//search Array
searchedName_Array=[[NSArray alloc]init];
//table View
_search_TableView.delegate=self;
_search_TableView.dataSource=self;
//search Bar
self.sc=[[UISearchController alloc]initWithSearchResultsController:nil];
self.search_TableView.tableHeaderView=self.sc.searchBar;
self.sc.searchResultsUpdater=self;
self.definesPresentationContext = YES;
self.sc.dimsBackgroundDuringPresentation = NO;
// self.sc.searchBar.delegate=self;
self.sc.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.sc.searchBar.placeholder=@"Search Name";
//dictionary
[_findImage_Dictionary removeAllObjects];
[_findCategory_Dictionary removeAllObjects];
_findImage_Dictionary=[[NSMutableDictionary alloc]initWithObjects:image_Array forKeys:name_Array];
_findCategory_Dictionary=[[NSMutableDictionary alloc]initWithObjects:category_Array forKeys:name_Array];
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSPredicate*predicate=[NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchController.searchBar.text];
[name_Array filteredArrayUsingPredicate:predicate];
searchedName_Array=[name_Array filteredArrayUsingPredicate:predicate];
NSLog(@"%@",searchedName_Array);
[self.search_TableView reloadData];
}
# pragma mark - UITableViewControllerDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.sc.active==YES) {
return searchedName_Array.count;
}
else{
return name_Array.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *identifier=@"SearchCell";
SearchTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell==nil) {
cell=[[SearchTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
CALayer* layer = cell.layer;
[layer setCornerRadius:8.0f];
[layer setMasksToBounds:YES];
[layer setBorderWidth:1.0f];
if (self.sc.active==YES) {
dispatch_async (dispatch_get_main_queue(), ^{
cell.searchName_Label.text=[searchedName_Array objectAtIndex:indexPath.row];
NSString *name=[searchedName_Array objectAtIndex:indexPath.row];
cell.search_Image.image=(UIImage*) [_findImage_Dictionary valueForKey:name];
cell.searchCategory_Label.text=[_findCategory_Dictionary valueForKey:name];
});
}
else{
dispatch_async (dispatch_get_main_queue(), ^{
cell.searchName_Label.text=[NSString stringWithFormat:@"%@",[name_Array objectAtIndex:indexPath.row]];
cell.searchCategory_Label.text=[NSString stringWithFormat:@"%@",[category_Array objectAtIndex:indexPath.row]];
cell.search_Image.image = (UIImage*) [image_Array objectAtIndex:indexPath.row];
});
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SearchTableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
_nameStr=selectedCell.searchName_Label.text;
NSLog(@"%@",_nameStr);
DetailViewController *destinationController = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailVC"];
NSLog(@"%@",_findCategory_Dictionary);
destinationController.name = _nameStr;
destinationController.imageDictionary=_findImage_Dictionary;
destinationController.categoryDictionary=_findCategory_Dictionary;
NSLog(@"%@",destinationController.categoryDictionary);
[self.navigationController pushViewController:destinationController animated:YES];
// DetailViewController *dvc=[self.storyboard instantiateViewControllerWithIdentifier:@"DetailVC"];
// [self presentViewController:dvc animated:YES completion:^{
// dvc.name=_nameStr;
// }];
}
@end
detailvc.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *detail_ImageView;
@property (strong, nonatomic) IBOutlet UILabel *detailName_Label;
@property (strong, nonatomic) IBOutlet UILabel *detailCategory_Label;
@property NSString *name;
@property NSMutableDictionary *imageDictionary,*categoryDictionary;
@end
detailvc.m
#import "DetailViewController.h"
#import "SearchViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *image=[_imageDictionary valueForKey:_name];
NSLog(@"%@",image);
_detailName_Label.text=_name;
_detailCategory_Label.text=[_categoryDictionary valueForKey:_name];
_detail_ImageView.image=(UIImage *)image;
NSLog(@"%@",_categoryDictionary);
NSLog(@"%@",_detailCategory_Label.text);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up here with your email
ConversionConversion EmoticonEmoticon