我希望在搜索后显示来自搜索栏的tableview中的数据。当我搜索一个表格视图时,显示一个结果。我想我在cellForRowAtIndexPath中犯了错误。任何人都可以检查我的代码卡在哪里..并帮助我这个。他/她将因此而受到高度赞赏。在此先感谢。
@implementation SearchBarView
{
NSMutableDictionary *jsonDict;
UITableView *searchTableView;
NSArray *nameOfPro;
NSMutableArray *nameArray;
NSString *passJson;
NSMutableDictionary *dataDict;
NSString *proTrackingNo;
}
@synthesize searchBar,proOfSearch;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"$$$+++++======%@",proOfSearch);
nameArray = [[NSMutableArray alloc]init];
// Do any additional setup after loading the view from its nib.
self.navigationItem.hidesBackButton = YES;
UIImage *backImage=[UIImage imageNamed:@"back.png"];
UIButton *back=[UIButton buttonWithType:UIButtonTypeCustom];
back.frame=CGRectMake(30,40,20,20);
[back setBackgroundImage:backImage forState:UIControlStateNormal];
[back addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnSidemenu=[[UIBarButtonItem alloc]initWithCustomView:back];
self.navigationItem.leftBarButtonItem=btnSidemenu;
searchTableView=[[UITableView alloc]init];
searchTableView.frame=CGRectMake(0, 0, 320, 520);
searchTableView.delegate=self;
searchTableView.dataSource=self;
[self.view addSubview:searchTableView];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 60, 320, 54)];
searchBar.delegate = self;
[self setSearchController:[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]];
[self.searchController setSearchResultsDataSource:self];
[self.searchController setSearchResultsDelegate:self];
[self.searchController setDelegate:self];
self.navigationItem.titleView = searchBar;
NSLog(@"My search Bar");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)backBtnAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", searchText];
nameOfPro = [proOfSearch filteredArrayUsingPredicate:resultPredicate];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [nameArray count];
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier=@"CellID";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.backgroundColor=[UIColor redColor];//[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:239.0/255.0 alpha:1.0];
}
[nameArray addObjectsFromArray:proOfSearch];
NSString *category = [nameArray objectAtIndex:indexPath.row];
cell.textLabel.text= category;
//[[nameArray objectAtIndex:indexPath.row]valueForKey:@"protracking"];
[nameOfPro objectAtIndex:indexPath.row];
[searchTableView reloadData];
return cell;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if (searchText.length>0)
{
searchText = [searchText stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"typed text == %@",searchText);
passJson = searchText;
NSPredicate *predicate =
[NSPredicate predicateWithFormat: @"SELF CONTAINS[cd] %@", searchText];
nameOfPro= [nameArray filteredArrayUsingPredicate:predicate];
[searchTableView reloadData];
}
else
{
nameOfPro=nameArray;
[searchTableView reloadData];
}
[self seacrhBar];
}
-(void)seacrhBar
{
NSUserDefaults *uidSave = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *get = [[NSMutableDictionary alloc]init];
[get setObject:[uidSave valueForKey:@"uid"] forKey:@"uid"];
[get setObject:passJson forKey:@"pro"];
NSLog(@"the search bar service %@",get);
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",searchBarUrl]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (responseData != nil)
{
jsonDict = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"Values =======%@",jsonDict);
dataDict = [jsonDict objectForKey:@"data"];
NSLog(@"My Data Dict %@",dataDict);
proTrackingNo = [dataDict valueForKey:@"protracking"];
NSLog(@"PRoTracking=== %@",proTrackingNo);
}
if (error)
{
NSLog(@"error %@",error.description);
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Server not responding" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil];
alertView.tag=13;
[alertView show];
}
}