我在表格视图中创建了一个具有搜索栏的自定义单元格。用户输入的文本将用于搜索SQLite数据库和表视图中显示的结果。我现在的问题是,如果我使用自定义单元格,搜索结果不会显示在表格视图中,但是当我使用默认单元格时,搜索结果会起作用。例如,在下面的代码中,如果我使用[cell setText:currentSubLocality];它将显示结果,但如果我使用cell.lblG.text = currentLocation;它将不会显示结果。请帮助,一直在努力2天。感谢
#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 :(得分:0)
试试这个会起作用。将以下代码粘贴到cellforrowatindexpath方法中,不要忘记在属性检查器中设置TableViewCell“Identifier”
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell==nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
答案 1 :(得分:-1)
我通过添加tableview作为插座解决了这个问题。然后更改以下行
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"vvv"];
到
CustomCell *cell = [customTableView dequeueReusableCellWithIdentifier:@"vvv"];