我正在尝试做searchBar,这是我的代码
AuthorsTableViewController.h
@interface AuthorsTableViewController : UITableViewController <UISearchBarDelegate, UISearchDisplayDelegate> {
NSMutableArray *authorsArray;
NSMutableArray *resultArray;
QuotesNavController *quotes;
}
@property (nonatomic, strong) IBOutlet UISearchBar *authorSearchBar;
@property (nonatomic, retain) NSMutableArray *authorsArray;
@property (nonatomic, retain) NSMutableArray *resultArray;
-(sqlite3 *) openDataBase;
-(IBAction)backToQuotes:(id)sender;
@end
AuthorsTableViewController.m
#import "AuthorsTableViewController.h"
@interface AuthorsTableViewController ()
@end
@implementation AuthorsTableViewController
@synthesize authorsArray, resultArray;
- (void)viewDidLoad
{
[super viewDidLoad];
[self retrieveDataFromSqlite];
resultArray =[[NSMutableArray alloc] initWithCapacity:[authorsArray count]];
[self.tableView reloadData];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)backToQuotes:(id)sender{
[self performSegueWithIdentifier:@"backToQuotes" sender:sender];
}
-(sqlite3 *) openDataBase{
sqlite3 *database;
NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"SuccessQuotes" ofType:@"sqlite"];
if (sqlite3_open([sqLiteDb UTF8String], &database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
}else{
NSLog(@"database opened!");
}
return database;
}
-(void) retrieveDataFromSqlite{
authorsArray = [[NSMutableArray alloc] init];
sqlite3 *myDatabase = [self openDataBase];
const char *sqlSelect = "SELECT *, COUNT(qu_author) AS count FROM authors JOIN quotes ON _auid = qu_author GROUP BY au_name ORDER BY au_name ASC";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(myDatabase, sqlSelect, -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int author_id = sqlite3_column_int(compiledStatement, 0);
NSString *au_name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 1)];
NSString *au_picture = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 2)];
int quotes_id = sqlite3_column_int(compiledStatement, 3);
NSString *quotes_content = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 4)];
int quotes_author = sqlite3_column_int(compiledStatement, 5);
NSString *quotes_favorite = [NSString stringWithUTF8String:(char *) sqlite3_column_text(compiledStatement, 6)];
int count = sqlite3_column_int(compiledStatement, 7);
Authors *authors = [[Authors alloc] initWithUniqueId:author_id au_name:au_name au_picture:au_picture quotes_id:quotes_id quotes_content:quotes_content quotes_author:quotes_author quotes_author:quotes_author quotes_favorite:quotes_favorite count:count];
[authorsArray addObject:authors];
} // while end
}// prepare end
sqlite3_finalize(compiledStatement);
sqlite3_close(myDatabase);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView)
{
NSLog(@"resultArray: %lu", (unsigned long)[resultArray count]);
return [resultArray count];
}
else
{
NSLog(@"authorsArray: %lu", (unsigned long)[resultArray count]);
return [authorsArray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"authorCell";
AuthorsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[AuthorsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Authors *temp_items = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
temp_items = [resultArray objectAtIndex:indexPath.row];
}
else
{
temp_items = [authorsArray objectAtIndex:indexPath.row];
}
[cell.authorName setText:[NSString stringWithFormat:@"%@",temp_items.au_name]];
[cell.count setTitle:[NSString stringWithFormat:@"%d",temp_items.count] forState:UIControlStateNormal];
UIImage *theImage = [UIImage imageNamed:temp_items.au_picture];
[cell.authorPic.layer setBorderColor: [[UIColor colorWithRed:232/255.0 green:216/255.0 blue:167/255.0 alpha:1] CGColor]];
[cell.authorPic.layer setBorderWidth: 2.0];
cell.authorPic.image = theImage;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
UIView *customColorView = [[UIView alloc] init];
customColorView.backgroundColor = [UIColor colorWithRed:93/255.0
green:45/255.0
blue:22/255.0
alpha:0.5];
cell.selectedBackgroundView = customColorView;
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 121;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Authors *temp_items = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
temp_items = [resultArray objectAtIndex:indexPath.row];
}
else
{
temp_items = [authorsArray objectAtIndex:indexPath.row];
}
quotes = [self.storyboard instantiateViewControllerWithIdentifier:@"QuotesNavController"];
quotes.quType = 3;
quotes.authorId = temp_items.author_id;
[self presentModalViewController:quotes animated:YES];
}
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
[resultArray removeAllObjects];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.au_name contains[c] %@", searchText];
resultArray = [NSMutableArray arrayWithArray:[authorsArray filteredArrayUsingPredicate:predicate]];
[self.tableView reloadData];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
return YES;
}
@end
我的问题是细胞正常显示如下
但是当我在searchBar中写一个单词时,它显示如下
我确信搜索在NSlog
的输出numberOfRowsInSection
中运行良好,但调用效果不佳。
希望有人能帮助我。