2012-10-07 23 views
0

我在具有搜索欄的表視圖中創建了一個自定義單元格。用戶輸入的文本將用於搜索SQLite數據庫並將結果顯示在表格視圖中。我現在遇到的問題是,如果我使用自定義單元格,但在使用默認單元格時,搜索結果不會顯示在表格視圖中。例如,在下面的代碼中,如果我使用[cell setText:currentSubLocality];它會顯示結果,但如果我使用cell.lblG.text = currentLocation;它將不顯示結果。請幫忙,一直在掙扎2天。謝謝XCode中的CustomCell不顯示值

#import "CustomViewController.h" 
#import "FMDBDataAccess.h" 
#import "locationCode.h" 
#import "CustomCell.h" 
@interface CustomViewController() 

@end 

@implementation CustomViewController{ 
    NSMutableArray *searchResults;} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

    return [searchResults count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vvv"]; 

    if (cell==nil){ 
     cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"vvv"]; 

    } 

    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     locationCode *code = [[locationCode alloc] init]; 
     code= [searchResults objectAtIndex:indexPath.row]; 
     NSString *currentLocation = code.location; 
     NSString *currentSubLocality = code.subLocality 

     //[cell setText:currentSubLocality]; 
     cell.lblG.text = currentLocation; 
     cell.lblL.text = currentSubLocality; 
    } 


    return cell; 

} 


- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope 
{ 
    NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"spr" ofType:@"sqlite"]; 
    FMDatabase *database = [FMDatabase databaseWithPath:databasePath]; 

    [database open]; 
    searchResults = [[NSMutableArray alloc] initWithObjects:nil count:0]; 

    NSString *query = [NSString stringWithFormat:@"select * from Locations where locationCode like '%@%%'", searchText]; 

    FMResultSet *results = [database executeQuery:query]; 

    while([results next]) { 

     locationCode *code = [[locationCode alloc] init]; 
     code.code = [results stringForColumn:@"locationcode"]; 
     code.subLocality= [results stringForColumn:@"sublocality"]; 
     code.longitude= [results stringForColumn:@"longitude"]; 
     code.latitude= [results stringForColumn:@"latitude"]; 

     [searchResults addObject:code]; 
    } 

    [database close]; 

} 

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
shouldReloadTableForSearchString:(NSString *)searchString 
{ 
    [self filterContentForSearchText:searchString 
           scope:[[self.searchDisplayController.searchBar scopeButtonTitles] 
             objectAtIndex:[self.searchDisplayController.searchBar 
                selectedScopeButtonIndex]]]; 

    return YES; 
} 

@end 
+0

我想你需要發佈您的customcell源代碼 – smoothdvd

+0

請調試第一 – makboney

回答

-1

我通過添加tableview作爲出口解決了這個問題。然後下面一行

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vvv"]; 

改變

CustomCell *cell = [customTableView dequeueReusableCellWithIdentifier:@"vvv"]; 
0

嘗試這個它會奏效。粘貼的cellForRowAtIndexPath方法下面的代碼,不要忘記設置你的TableViewCell「標識符」在屬性檢查器中

static NSString *CellIdentifier = @"Cell"; 
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell==nil) { 
     cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    }