У меня есть представление с табличным представлением и панелью поиска.
Мое табличное представление заполнено данными, взятыми в SQLite, теперь я бы добавил панель поиска.
Поля базы данных вызываются через объект.
Receip.h
@interface Receip : NSObject {
int ID; //id receip
NSString *name;
NSString *ingredients;
NSString *detail;
NSString *image;
}
В то время как мой SearcViewController:
#import "Receip.h"
#import "DBAccess.h"
#import "SearchViewController.h"
#import "DetailViewController.h"
@implementation SearchViewController
@synthesize table,search, dataSource, tableData;
- (void)viewDidLoad {
[super viewDidLoad];
//initialize the two arrays; dataSource will be initialized and populated
tableData = [[NSMutableArray alloc]init];
dataSource = [[NSMutableArray alloc] init];
// DBAccess object
DBAccess *dbAccess = [[DBAccess alloc] init];
// insert all recipes in the array
dataSource = [dbAccess getAllRecipes];
if ([dataSource count] > 0){
[tableData addObjectsFromArray:dataSource];
//on launch it should display all the records
[table reloadData];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:identifier] autorelease];
}
// Configure the cell.
// Get the Receip object
Receip *receip = [tableData objectAtIndex:indexPath.row];
cell.textLabel.text = receip.name;
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
cell.textLabel.numberOfLines = 2;
cell.detailTextLabel.text = receip.detail;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void) searchTableView {
NSString *searchText = search.text;
NSMutableArray *productArray = [[NSMutableArray alloc] init];
for (int i = 0; i < [dataSource count]; i++)
{
Receip *receip = [dataSource objectAtIndex:i];
NSString* sTemp = receip.name;
NSRange titleResultsRange = [sTemp rangeOfString:searchText
options:NSCaseInsensitiveSearch];
if (titleResultsRange.length > 0)
[productArray addObject:product];
}
[tableData release];
tableData = [[NSMutableArray alloc] initWithArray:productArray];
[productArray release];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if([searchText length] > 0)
{
searchFlag = YES;
[self searchTableView];
}
else
{
searchFlag = NO;
}
[table reloadData];
}