-
-
Save himanshu-benzatine/716fd8d7ca035a5c0a01c87df48915bf to your computer and use it in GitHub Desktop.
Here is code for simple searching with array .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// Simplesearching | |
// | |
// Created by Benzatine Infotech on 10/13/16. | |
// Copyright © 2016 Benzatine Infotech. All rights reserved. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
{ | |
NSArray *searchresult; | |
NSArray *record; | |
BOOL *search; | |
} | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
search = 0; | |
searchresult = [[NSArray alloc]init]; | |
record = [[NSMutableArray alloc] init]; | |
record = @[@{@"id":@"1", @"name":@"Hems Moradiya"}, @{@"id":@"2" , @"name":@"patel hiren"},@{@"id":@"3" , @"name":@"payal patel"},@{@"id":@"4" , @"name":@"komal sukhadhiya"},@{@"id":@"5" , @"name":@"bhavesh chohan"},@{@"id":@"6" , @"name":@"nilam jasoliya"},@{@"id":@"7" , @"name":@"pavan parekh"},@{@"id":@"8", @"name":@"himanshu Moradiya"}, @{@"id":@"9" , @"name":@"dipali hiren"},@{@"id":@"10" , @"name":@"radhi patel"},@{@"id":@"11" , @"name":@"jayesh sukhadhiya"},@{@"id":@"12" , @"name":@"kalu chohan"},@{@"id":@"13" , @"name":@"gotalu jasoliya"},@{@"id":@"14" , @"name":@"anderson parekh"}]; | |
// Do any additional setup after loading the view, typically from a nib. | |
} | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
if (search) { | |
return [searchresult count]; | |
} | |
else{ | |
return record.count; | |
} | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *MyIdentifier = @"MyIdentifier"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier forIndexPath:indexPath]; | |
UILabel *lblname = (UILabel*)[cell viewWithTag:1]; | |
UILabel *number1 = (UILabel*)[cell viewWithTag:2]; | |
if (search) { | |
number1.text = [[searchresult valueForKey:@"id"] objectAtIndex:indexPath.row]; | |
lblname.text = [[searchresult valueForKey:@"name"] objectAtIndex:indexPath.row]; | |
}else{ | |
number1.text = [[record valueForKey:@"id"] objectAtIndex:indexPath.row]; | |
lblname.text = [[record valueForKey:@"name"] objectAtIndex:indexPath.row]; | |
} | |
return cell; | |
} | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (search) { | |
NSLog(@"search result selected id and name are : %@ %@",[[searchresult valueForKey:@"id"] objectAtIndex:indexPath.row],[[searchresult valueForKey:@"name"] objectAtIndex:indexPath.row]); | |
}else{ | |
NSLog(@"simple table selected id and name %@ %@",[[record valueForKey:@"id"] objectAtIndex:indexPath.row],[[record valueForKey:@"name"] objectAtIndex:indexPath.row]); | |
} | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
- (IBAction)txtsearchaction:(id)sender { | |
search = 1; | |
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", _txtsearch.text]; | |
NSLog(@"%@",resultPredicate); | |
searchresult = [record filteredArrayUsingPredicate:resultPredicate]; | |
NSLog(@"%@",searchresult); | |
if (searchresult.count > 0) { | |
}else{ | |
search = 0; | |
} | |
[self.tableview reloadData]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment