2012-01-23 3 views
0

테이블 뷰와 검색 창이있는보기가 있습니다. 내 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]; 
} 
+0

예 해당 기능이 작동하지 않습니까? – vishy

+0

내가 개체 필드 내에서 검색을 모르기 때문에 작동하지 않습니다 – Maurizio

답변

1

좋아, 여기에 u는 2 개 배열을 유지해야합니다. 검색된 목록의 전체 목록 1은 1입니다. u는 점에서 개체를 얻을 수있는 방법이

- (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]; 
} 

및 U가 사용되는 배열 알게 수 _searchFlag에서 행 방법 셀

, & 구현.

+0

Thk. 나는 _ReleaseObj (_filteredArr)의 사용을 이해하지 못한다. _filteredArr = [[NSArray alloc] initWithArray : productArray]; – Maurizio

+0

내가 편집 한 NSArray NSArray를 유지하기위한 NSArray는 새로운 배열을 만들었고 alloc & initialize했다. 그 이유는이 메소드가 다시 호출 되었기 때문에 & old는 검색 목록을 해제하고 다시 채우기 때문입니다. – vishy

+0

알았어, 네. 어쩌면 내 코드에서 _filteredArray tableData = [[NSMutableArray alloc] init]; 권리? – Maurizio

관련 문제