可以在对象内搜索

时间:2012-01-23 08:26:48

标签: iphone objective-c ios

我有一个带有tableview和搜索栏的视图。 我的tableview充满了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];
}

1 个答案:

答案 0 :(得分:1)

好的,这里需要维护2个阵列。 1表示搜索列表的完整列表1。实施这些方法

- (void) searchTableView 
{
    NSString *searchText = _searchBar.text;
    NSMutableArray *productArray = [[NSMutableArray alloc] init];
        for (int i = 0; i < [_productList count]; i++)
    {
       StoreProduct* product = [_productList objectAtIndex:i];    
        NSString* sTemp = product.name;
        NSRange titleResultsRange = [sTemp rangeOfString:searchText     options:NSCaseInsensitiveSearch];
        if (titleResultsRange.length > 0)
            [productArray addObject:product];        
    } 
    [_filteredArr release];
    _filteredArr = [[NSArray alloc] initWithArray:productArray];
   [productArray release];
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if([searchText length] > 0) 
    {
        _searchFlag = YES;
        [self searchTableView];
    }
    else 
    {
        _searchFlag = NO;
    }
    [_catalogTableView reloadData];
}

并且在_searchFlag的行方法中,你可以知道要使用哪个数组,&amp;你可以得到那个对象。