VyTcdc VyTcdc - 5 months ago 9
Objective-C Question

When I touch in tableview How to get all values Back in tableview in search

I trying to search a particular name in an array, It is working well, But I want to cancel search by touching in other place in tableview, How to do that.

I trying to search a particular name in an array, It is working well, But I want to cancel search by touching in other place in tableview, How to do that.

- (void)viewDidLoad {
[super viewDidLoad];

// arr1=[[NSMutableArray alloc]initWithObjects:@"Raj",@"Ramu",@"Abdul",@"Kavitha",@"Abi",@"Abinesh",@"Gobi",@"Giri",@"Gayathri",@"Ramesh",@"Thiru",@"Sri",@"Siva",@"Senthil",@"Kumar",@"Rajkumar",@"Raman",@"Karthik",@"Kanna",@"Kamal",@"Ramya",@"Somu",@"Sankar",@"Murali",@"Muthu",@"Murugan",@"Nandha",@"Kamal", nil];
// NSLog(@"%@",arr1);


NSURL *url=[NSURL URLWithString:@"http://192.168.1.92/Abdul/Json/Json5.txt"];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
conn=[NSURLConnection connectionWithRequest:req delegate:self];


if(conn)
{
webdata =[[NSMutableData alloc]init];
NSLog(@"Connection Success");
}

//[tabvw reloadData];


// Do any additional setup after loading the view, typically from a nib.
}

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



-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

[webdata setLength:0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

[webdata appendData:data];

}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{


NSLog(@"%@",@"Fail");

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{


jsonarr=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];


if (jsonarr> 0)
{
[tabvw reloadData];

}



}


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

return 1;

}

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



if (isfiltered)
{
return [filterarr count];
}


else
{
return [jsonarr count];

}


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


UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"myid"];


if (cell==nil)

{

cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"myid"];

}


if (isfiltered ) {


dic =[filterarr objectAtIndex:indexPath.row];
cell.textLabel.text=[dic objectForKey:@"Name"];

}

else
{

dic =[jsonarr objectAtIndex:indexPath.row];
cell.textLabel.text=[dic objectForKey:@"Name"];

}

return cell;
}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{





NSPredicate *pred =[NSPredicate predicateWithFormat:@"Name contains[c]%@",searchText];

filterarr = [jsonarr filteredArrayUsingPredicate:pred];

NSLog(@"%@",filterarr);

isfiltered=YES;
[tabvw reloadData];

}


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

[sbar resignFirstResponder];
}

Answer

Try This,

ViewDidLoad

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshData:)];
tapGesture.delegate = self;
[self.view addGestureRecognizer:tapGesture];


#pragma mark Gesture delegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
if (touch.view == self.view)
    return YES;
return NO;
}

- (IBAction)refreshData:(id)sender
{
 isfiltered=NO;
 [tabvw reloadData]; 
}
Comments