2016-06-30 1 views
1

배열의 특정 이름을 검색하려고하면 잘 작동하지만 Tableview에서 다른 곳을 터치하여 검색을 취소하고 싶습니다. 그렇게.내가 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]; 
} 

답변

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

감사합니다 귀하의 요구 사항에 따라 당신이 "있는 tableview"로 등 "self.view"를 변경할 수 – VyTcdc

+0

@VyTcdc을 확인합니다 – Ravindhiran

+0

검색에서 (x) 십자 버튼을 클릭하는 동안 동일한 작업을 수행하려고 할 때 알려주시겠습니까? – VyTcdc

관련 문제