来自ios中sqlite数据库的UISearch记录

时间:2015-03-25 05:27:50

标签: ios sqlite

我的问题是UISearchDisplay控制器使用来自sqlite数据库的搜索记录。我的查询是来自sqlite数据库的UISearch条搜索记录正确显示mytableview,我的查询是特定的行选择并在第二个视图控制器中传递不同的数据,所以我传递数据到索引超出绑定错误..所以任何帮助我

#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

0 个答案:

没有答案