2016-06-30 3 views
1

配列内の特定の名前を検索しようとしていますが、うまくいきますが、テーブルビューの他の場所をタッチして検索を取り消したいそれを行う。テーブルビューでタッチするとすべての値を取得する方法テーブルビューで検索するに戻る

配列内の特定の名前を検索しようとしていますが、うまくいきますが、テーブルビューの他の場所に触れることで検索をキャンセルしたいと思います。

- (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]; 
} 

答えて

2

の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]; 
} 
+0

あなたがなど "テーブルビュー" に "self.view" を変更することができ、あなたの要件ごとに感謝 – VyTcdc

+0

@VyTcdcをチェックします – Ravindhiran

+0

検索で利用可能な(x)十字ボタンをクリックしているときに同じことをしたいときに教えてもらえますか? – VyTcdc

関連する問題