当我在 tableview 中触摸时如何获取所有值在搜索中返回 tableview
When I touch in tableview How to get all values Back in tableview in search
我试图在数组中搜索特定名称,它运行良好,但我想通过触摸 tableview 中的其他位置来取消搜索,如何做到这一点。
我试图在数组中搜索特定名称,它运行良好,但我想通过触摸 tableview 中的其他位置来取消搜索,如何做到这一点。
- (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];
}
让我们试试这个并检查输出,
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];
}
我试图在数组中搜索特定名称,它运行良好,但我想通过触摸 tableview 中的其他位置来取消搜索,如何做到这一点。
我试图在数组中搜索特定名称,它运行良好,但我想通过触摸 tableview 中的其他位置来取消搜索,如何做到这一点。
- (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];
}
让我们试试这个并检查输出,
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];
}