2012-07-09 41 views
0

我試圖創建的Xcode 4.3.3搜索引擎,搜索欄搜索一個UITableView一個SQLite查詢的值分配給Nsmutable陣列

首先,我想沒有動態數據庫,並沒有sqlite的連接創建的搜索引擎,它的工作,如果在NSMutableArray預定義的靜態元素之間尋找我有能力。

現在,我不想在數組中靜態預定義元素,而想訪問sqlite數據庫並執行查詢,將結果作爲單元格字段。 我試圖用FMDB連接,但問題是我無法實現。

這是我的代碼,我做錯了什麼?

// 
// FilterDemoTableViewController.m 
// Created by Elias Rahme. 
// Copyright (c) ERC. 
// 

#import "ViewController.h" 
#import "DetailsViewController.h" 
#import "Food.h" 
#import "FMDatabase.h" 

@implementation ViewController 

@synthesize allTableData; 
@synthesize filteredTableData; 
@synthesize searchBar; 
@synthesize isFiltered; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    searchBar.delegate = (id)self; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"]; 
    FMDatabase *db = [[FMDatabase alloc] initWithPath:path]; 
    [db open]; 
    if (![db open]) { 
     NSLog(@"Could not open db."); 
    } 
    else{ 
    NSLog(@"Database is opened"); 
    } 
    FMResultSet *fResult= [db executeQuery:@"SELECT a_id FROM Description where desc_id=1"]; 
    if(!fResult){ 

     NSLog(@"Could not execute query"); 
     } 
    else{ 
     NSLog(@"Query executed"); 
    } 
    eleData = [fResult stringForColumn:@"desc"]; 

    // NSLog([fResult stringForColumn:@"desc"]); 

    allTableData = [[NSMutableArray alloc] initWithObjects: 
        [[Food alloc] initWithName:@"test" andDescription:@"TEst" ], 
        [[Food alloc] initWithName:@"Steak" andDescription:@"Medium"], 
        [[Food alloc] initWithName:@"Salad" andDescription:@"Caesar"], 
        [[Food alloc] initWithName:@"Salad" andDescription:@"Bean"], 
        [[Food alloc] initWithName:@"Fruit" andDescription:@"Apple"], 
        [[Food alloc] initWithName:@"Potato" andDescription:@"Baked"], 
        [[Food alloc] initWithName:@"Potato" andDescription:@"Mashed"], 
        [[Food alloc] initWithName:@"Bread" andDescription:@"White"], 
        [[Food alloc] initWithName:@"Bread" andDescription:@"Brown"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
        [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
        nil ]; 
    [db close]; 

} 
/* 
    NSLog(@"Data from ele.h file"); 


    /* Now here we have to write the code from the database to display the details */ 
    /* 
    aElephant = [[NSMutableArray alloc] init]; 
    NSLog(@"Data base is entering"); 

    /* 
    Here we have to set the path 
    We are creating the path for the datbase file in order to access 
    */ 
/* 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"]; 

    NSLog(@"No problem with the path"); 

    /* FMDatabase here is used to take the database path */ 
/* 
    FMDatabase *db = [[FMDatabase alloc] initWithPath:path]; 

    /* Here we are opening the datase in order to access it */ 
/* 
    [db open]; 
    NSLog(@"Database Opened"); 

    /* 
    Then the next step here is taking the database result using the sqlquery and then carrying that result into the resultset object 
    Resultset for Elephant: select * from Description where a_id=1; here description is the table, a_id is the id of Elephant it is 
    the primary key in animals table 
/* */ 
/* NSLog(@"NOt yet executing the query"); 
    FMResultSet *fResult= [db executeQuery:@"SELECT * FROM description"]; 
    NSLog(@"Did Execute the Query"); 
    // NSLog(*fResult); 

    //Now we have to take these results in to a loop and then repeteadly loop it in order to get all the data in a our Array Object 

    while([fResult next]) 
    { 
    /* /* taking results from database to a string "eleData" */ 
    /* eleData = [fResult stringForColumn:@"desc"]; 
     /* adding data from the string object to Array */ 
     //[aElephant addObject:eleData]; 
     /* Checking weather data has come or not */ 
    // NSLog(@"The data is %@=",eleData); 
    //} 
    /* Closing the Database */ 
    //[db close]; 


- (void)viewDidUnload 
{ 
    [self setSearchBar:nil]; 
    [super viewDidUnload]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    [super viewWillDisappear:animated]; 
} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int rowCount; 
    if(self.isFiltered) 
     rowCount = filteredTableData.count; 
    else 
     rowCount = allTableData.count; 

    return rowCount; 
    // return [aElephant count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

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

     /* 
    if(isFiltered) 
     eData = [filteredTableData objectAtIndex:indexPath.row]; 
    else 
     eData = [allTableData objectAtIndex:indexPath.row]; 
*/ 
    // NSLog(aElephant); 
/* NSString *eData = [aElephant objectAtIndex:indexPath.row]; 
    cell.textLabel.text = eData; 
    return cell;*/ 


    Food* food; 
    if(isFiltered) 
     food = [filteredTableData objectAtIndex:indexPath.row]; 
    else 
     food = [allTableData objectAtIndex:indexPath.row]; 

    cell.textLabel.text = food.name; 
    cell.detailTextLabel.text = food.description; 


    return cell; 
} 

#pragma mark - Table view delegate 

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text 
{ 
    if(text.length == 0) 
    { 
     isFiltered = FALSE; 
    } 
    else 
    { 
     isFiltered = true; 
     filteredTableData = [[NSMutableArray alloc] init]; 

     for (Food* food in allTableData) 
     { 
      NSRange nameRange = [food.name rangeOfString:text options:NSCaseInsensitiveSearch]; 
      NSRange descriptionRange = [food.description rangeOfString:text options:NSCaseInsensitiveSearch]; 
      if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound) 
      { 
       [filteredTableData addObject:food]; 
      } 
     } 
    } 

    [self.tableView reloadData]; 
} 


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    [self showDetailsForIndexPath:indexPath]; 
} 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self showDetailsForIndexPath:indexPath]; 
} 

-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath 
{ 
    [self.searchBar resignFirstResponder]; 
    DetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"]; 
    Food* food; 

    if(isFiltered) 
    { 
     food = [filteredTableData objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     food = [allTableData objectAtIndex:indexPath.row]; 
    } 

    vc.food = food; 
    [self.navigationController pushViewController:vc animated:true];  
} 

@end 

總之,而不是initWithName對具有靜態價值,我希望它有從SQLite數據庫的值。 任何幫助將不勝感激 謝謝你的幫助。

+0

什麼是'eleData'的類型,它在查詢後包含了什麼? – 2012-07-09 15:18:17

+0

嘗試'eleData = [fResult stringForColumn:@「a_id」];'。 – 2012-07-09 15:31:06

+0

它沒有工作,有沒有其他方法? – 2012-07-10 05:17:45

回答

0

問題是我沒有回來,然後正確訪問數據庫,並沒有看到發生的變化,因爲我打開我的本地數據庫時,我應該打開我的文檔中的iPhone模擬器。