mardi 24 mars 2015

uisisearch search record from sqlite database in ios

My question is uisearchdisplay controller using search record from sqlite database. and my quarry is uisearch bar search record from sqlite database properly display mytableview and my quary is perticular row select and pass different data in second view controller so i am passing data to index beyond bound error.. so any help me



enter code here

#import "SearchViewController.h"


import "SWRevealViewController.h"


import "ZpaleoViewController.h"


@interface SearchViewController ()


@end


@implementation SearchViewController @synthesize strSearch,tblview,searchResult;



- (void)viewDidLoad {
[super viewDidLoad];
SWRevealViewController *revealViewController = self.revealViewController;
if ( revealViewController )
{
[self.slide6 setTarget: self.revealViewController];
[self.slide6 setAction: @selector( revealToggle: )]
[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
}

appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
databse = [FMDatabase databaseWithPath:appdelegate.appDBPath];

[arrname removeAllObjects];
[arrPaleo removeAllObjects];
[searchResult removeAllObjects];

arrname = [[NSMutableArray alloc] init];
arrPaleo = [[NSMutableArray alloc]init];
searchResult = [[NSMutableArray alloc]init];

[self searchRecord];

}




-(void)searchRecord{


path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
docsPath = [path objectAtIndex:0];
dbpath = [docsPath stringByAppendingPathComponent:@"PaleoData.sqlite"];

databse = [FMDatabase databaseWithPath:dbpath];
[databse setLogsErrors:TRUE];

[databse open];
NSString *selectQuery = [NSString stringWithFormat:@"select * from ZFOOD"];

FMResultSet *resultQuary = [databse executeQuery:selectQuery];


while ([resultQuary next]) {


NSString *z_cateory = [NSString stringWithFormat:@"%@", [resultQuary stringForColumn:@"ZNAME"]];


[arrname addObject:z_cateory];


}
[databse close];

}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{


NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText];
searchResult = [NSMutableArray arrayWithArray: [arrname filteredArrayUsingPredicate:resultPredicate]];



}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString


{ [self filterContentForSearchText:searchString scope: [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];



return YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResult count];

} else {
return [arrname count];
}


}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{



static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}


if (tableView == self.searchDisplayController.searchResultsTableView)
{

cell.textLabel.text = [searchResult objectAtIndex:indexPath.row];

cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"No.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];


}
else
{
cell.textLabel.text = [arrname objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Yes_check.png"]];
[cell.accessoryView setFrame:CGRectMake(0, 0, 15, 15)];
}

cell.backgroundColor = [UIColor colorWithRed:99.0/255 green:170.0/255 blue:229.0/255 alpha:1.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font=[UIFont fontWithName:@"Marker Felt" size:15];

return cell;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[self.searchDisplayController setActive:YES animated:NO];
searchResult = nil;
[tblview reloadData];

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

[databse open];
NSString *selectQuery = [NSString stringWithFormat:@"select ZPALEO from ZFOOD where ZNAME = '%@'",[searchResult objectAtIndex:indexPath.row]];

FMResultSet *resultQuary = [databse executeQuery:selectQuery];


// [arrPaleo removeAllObjects]; while ([resultQuary next]) {



NSString *z_paleo = [NSString stringWithFormat:@"%@", [resultQuary stringForColumn:@"ZPALEO"]];

[arrPaleo addObject:z_paleo];

}

[databse close];

ZpaleoViewController *objZpalieo = [self.storyboard instantiateViewControllerWithIdentifier:@"paleo"];

NSString *objstr1 = [arrPaleo objectAtIndex:indexPath.row];
objZpalieo.strpaleo = objstr1;
[self.navigationController pushViewController:objZpalieo animated:YES];


}




@end

Aucun commentaire:

Enregistrer un commentaire