2011-01-18 9 views
0

私のコードでこのセグメントに何が問題になったのかを調べるために数日間でした:( データが抽出された通常のUITableViewUITableViewの選択された行は、次のTabBarViewControllerで別の結果を表示します

しかし、私はこの類似したコードを使用して、検索の結果を表示する(私は複数のカテゴリの検索をしようとしましたが失敗しました)、エラーが発生しています、TabBarViewController上の選択された行と結果や結果の間の違い。私は本当にトンを支援する必要がある

私のプロジェクトが来週になる予定です。続き

TabBarViewControllerへの結合のために私のコードです。どんな助力も深く感謝します。

///////////////////////////////////////////////////////////////////////////// ///////////////////////////////

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 



    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 



    TabBarViewController *tabBarView = [[TabBarViewController alloc] initWithNibName:@"TabBarViewController" bundle:[NSBundle mainBundle]]; 

    Attraction *att = [attractions objectAtIndex: indexPath.row]; 


    tabBarView.attraction = att; 

    [self.navigationController presentModalViewController:tabBarView animated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [tabBarView release]; 

    } 


Thanks! 

Here are my full codes for you to inspect. =(

-(void) checkAndCreateDatabase{ 

    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    databaseName = @"funsg.sql"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 

    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 
    //NSLog([NSString stringWithFormat:@"GetData %@", databasePath]); 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 
    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    [fileManager release]; 

} 

-(void)createEditableCopyOfDatabaseIfNeeded { 

    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"abc.sql"]; 
    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) return; 
    // The writable database does not exist, so copy the default to the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"abc.sql"]; 
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error]; 
    if (!success) { 
     NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    } 
} 

-(void) readAttractionsFromDatabase { 
    [self checkAndCreateDatabase]; 


    // Open the database 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = [[NSString stringWithFormat:@"select * from everything "] UTF8String]; 
     //NSLog([NSString stringWithFormat:@"select * from everything"]); 

     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 
       NSString *bus  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; 
       NSString *desc  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *location = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSString *mrt  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 
       NSString *name  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)]; 
       NSString *image  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 5)]; 
       NSString *type  = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 6)]; 
       NSString *carpark = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; 

       // Create a new animal object with the data from the database 
       Attraction *att = [[Attraction alloc] initWithName:desc buses:bus add:location type:type mrt:mrt image:image name:name carpark:carpark]; 

       if (attractions == NULL) 
        // There should not be a NULL name 
        NSLog(@"Null name!!"); 
       else { 

        [attractions addObject:att]; 
        // Apparently the addObject function in NSMutableArray does not 
        // keep a copy of our object, so, we can't release it. 
        //[name release]; 
        [att release]; 
       } 
      } 
      sqlite3_finalize(compiledStatement); // Cleanup the statement 
     } 
     else { 
      NSLog(@"Error retrieving data from database."); 
     } 
     sqlite3_close(database); 
    } 
    else { 
     NSLog(@"Error: Can't open database!"); 
    } 
} 
-(void)viewDidLoad { 
    [super viewDidLoad]; 
    attractions = [[NSMutableArray alloc] init]; 
    searchedNames = [[NSMutableArray alloc] init]; 
    [self loadData]; 

} 

-(void) insertToDB :(Attraction*) att { 
    [self checkAndCreateDatabase]; 

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     static sqlite3_stmt *compiledStatement; 
     sqlite3_exec(database, [[NSString stringWithFormat:@"INSERT INTO everything (Bus,Description,Location,MRT,Name,image,type,Carpark) SELECT '%@','%@','%@','%@', '%@', '%@', '%@', '%@' WHERE NOT EXISTS (SELECT 1 FROM everything WHERE Name = '%@');", att.buses,att.desc,att.add, att.mrt, att.name, att.image, att.type , att.carpark,att.name] UTF8String], NULL, NULL, NULL); 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database);  
} 

-(void) loadData { 
    //First fetch the data from the JSON 
    NSURL *url = nil; 

    NSString *querystring = [NSString stringWithFormat:@"http://mp15.bitproj1.com/testsearch.php"]; 
    url = [NSURL URLWithString:querystring]; 


    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url]; 

    //NSLog(@"jsonreturn"); // Look at the console and you can see what the restults are 

    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding]; 
    NSError *error = nil; 

    // In "real" code you should surround this with try and catch 
    self.data = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error]; 
    if (data==nil){ 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Unable to update the data." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
     [alert show]; 
     [alert release]; 
    }  

    else 
    { 
     self.rows =[data objectForKey:@"attractions"]; 

     for (int i=0; i<[self.rows count]; i++) { 
      NSDictionary *dict = [rows objectAtIndex: i]; 
      Attraction* a = [[Attraction alloc] initWithName:[dict objectForKey:@"Description"] 
                 buses:[dict objectForKey:@"Bus"] 
                 add:[dict objectForKey:@"Location"] 
                 type:[dict objectForKey:@"type"] 
                 mrt:[dict objectForKey:@"MRT"] 
                 image:[dict objectForKey:@"image"] 
                 name:[dict objectForKey:@"Name"] 
                carpark:[dict objectForKey:@"Carpark"]]; 

      //Here we insert the data, when inserting we check for the duplicates. If the record already exists we do not insert. Code also must be optimized later 
      [self insertToDB:a]; 
     } 
    } 


    [jsonreturn release]; 

    [self readAttractionsFromDatabase]; 
} 



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

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return([searchedNames 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] autorelease]; 
    } 


    NSString *cellText = [searchedNames objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:cellText]; 
    return cell; 
} 

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
} 

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar { 
} 

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { 

    [searchedNames removeAllObjects];// remove all data that belongs to previous search 
    if([searchText isEqualToString:@""] || searchText==nil) { 
     // Nothing to search, empty result. 
     [myTableView reloadData]; 
     return; 
    } 

    for (NSString *att in attractions) { 
     Attraction* p = ((Attraction *)att); 
     NSRange r = [p.name rangeOfString:searchText options:NSCaseInsensitiveSearch]; 
     if(r.location != NSNotFound) { 
      [searchedNames addObject:p.name]; 
     } 
    } 
    [myTableView reloadData]; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
} 

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [searchBar resignFirstResponder]; 
} 

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 



    [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 



    TabBarViewController *tabBarView = [[TabBarViewController alloc] initWithNibName:@"TabBarViewController" bundle:[NSBundle mainBundle]]; 

    Attraction *att = [attractions objectAtIndex: indexPath.row]; 


    tabBarView.attraction = att;  

    [self.navigationController presentModalViewController:tabBarView animated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [tabBarView release]; 

} 
-(void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 
} 

-(void)viewDidUnload { 

} 

-(void)dealloc { 
    [data release]; 
    [attractions release]; 
    [super dealloc]; 

} 


@end 

答えて

0

データソースとして2つの異なる配列を使用していますか?もしそうなら、あなたは正しい配列から値を取得していないと思います。

そしてまた、私はこのコードの使用を理解カント:

[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES]; 

[self.tableView deselectRowAtIndexPath:indexPath animated:YES]; 

EDIT、とあなたはそのコードを変更することができます:あなたは2つの差分アレイを使用している

searchedNames、アトラクション。問題があります。

if(searching==YES) 

{ 

//retrieve the values from searchedNames array 

} 

else 

{ 

//retrieve the values from attractions array 

} 


-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { 
searching=YES; 
} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar { 
searching=NO; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if(searching==YES) 
     return([searchedNames count]); 
    else 
     return [attractions 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] autorelease]; 
    } 
    NSString *cellText; 
    if(searching==YES) 
     cellText = [searchedNames objectAtIndex:indexPath.row]; 
    else 
     cellText = [attractions objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:cellText]; 
    return cell; 
} 
+0

私の知る限り、他の配列はありません。しかし、ちょうど確かに、私はあなたが見えるように私のコードを貼り付けました:) –

+0

実際には、検索結果(不正確です)は常に同様の方法で表示されるように見えました。 : –

+0

あなたの検索ロジックコードを投稿することはできますか? – KingofBliss

関連する問題