2016-06-30 7 views
1

Ich versuche, einen bestimmten Namen in einem Array zu suchen, es funktioniert gut, aber ich möchte die Suche durch Berühren an anderer Stelle in der Tabellenansicht, Wie Sie abbrechen TU das.Wenn ich in der Tabellenansicht anfasse Wie bekomme ich alle Werte Zurück in der Tabellenansicht in der Suche

Ich versuche, einen bestimmten Namen in einem Array zu suchen, es funktioniert gut, aber ich möchte die Suche durch Berühren an anderer Stelle in der Tabellenansicht, wie das zu tun, abbrechen.

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

Antwort

2

lassen Sie uns dies versuchen und Ausgabe überprüfen,

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

Dank – VyTcdc

+0

@VyTcdc prüft nach Ihren Anforderungen können Sie "self.view" in "Tableview" usw. ändern etc .. – Ravindhiran

+0

können Sie mir sagen, wenn ich das gleiche tun möchte, während ich auf die (x) Kreuz-Schaltfläche in der Suche klicken. – VyTcdc

Verwandte Themen