2014-03-03 60 views
-1

我是新手编程。我正在构建一个扫描条形码并将UPC编号放入我的表格视图中的搜索框中的程序。现在我想要做的就是创建一个搜索扫描的UPC。 viewTable显示来自sqlite数据库的清单。 搜索栏是我所需要做的就是编程它搜索,我还没有找到任何关于如何做到这一点的好教程。我怎样才能实现这个?如何在iOS 7 tableView中实现搜索栏?

#import "MasterViewController.h" 
#import "DetailViewController.h" 
#import "ScanViewController.h" 
#import <sqlite3.h> 


@interface MasterViewController() 
{ 
    NSMutableArray *_objects; 

} 
@end 

@implementation MasterViewController 
{ 


} 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    // Get the DBAccess object; 
    DBAccess *dbAccess = [[DBAccess alloc] init]; 
    // Get the products array from the database 
    self.products = [dbAccess getAllProducts]; 
    // Close the database because we are finished with it 
    [dbAccess closeDatabase]; 
} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)insertNewObject:(id)sender 
{ 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

#pragma mark - Table View 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.products count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell. 
    cell.accessoryType = UITableViewCellAccessoryNone; 

    Product* product = [self.products objectAtIndex:[indexPath row]]; 
    cell.textLabel.text = product.serial; 

    return cell; 


} 


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return NO; 
} 


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [_objects removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view. 
    } 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([[segue identifier] isEqualToString:@"showDetail"]) 
    { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 

     Product *theProduct = _products[indexPath.row]; 
     [[segue destinationViewController] setDetailItem:theProduct]; 
     NSLog(@"this is prepare for segue"); 
    } 


} 

- (void)scanditSDKOverlayController: (ScanditSDKOverlayController *)scanditSDKOverlayController 
        didScanBarcode:(NSDictionary *)barcodeResult 
{ 
    // add your own code to handle the barcode result e.g. 

    scanResult = [barcodeResult valueForKey:@"barcode"]; 
    //barcodelabel.text = [NSString stringWithFormat:@"The barcode is: %@", scanResult]; 
    searchLabel.text = [NSString stringWithFormat:@"%@", scanResult]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top); 



    [searchLabel becomeFirstResponder]; 
    [super viewDidAppear:YES]; 

} 



- (void)scanditSDKOverlayController: (ScanditSDKOverlayController *)scanditSDKOverlayController 
       didCancelWithStatus:(NSDictionary *)status 
{ 
    // add your own code to handle the user canceling the barcode scan process 

} 


- (void)scanditSDKOverlayController: (ScanditSDKOverlayController *)scanditSDKOverlayController 
        didManualSearch:(NSString *)input 
{ 
    // add your own code to handle user input in the search bar 
    // (only required if you use the search bar provided by the Scandit SDK 
} 



-(IBAction)scanClick:(id)sender; 
{ 
    ScanditSDKBarcodePicker *picker = [[ScanditSDKBarcodePicker alloc] initWithAppKey:@"oK1ckH/7EeOPhaseNDXmvNVu+pCX2y6UHZWZ/VYw0hE"]; 

    picker.overlayController.delegate = self; 

    [picker startScanning]; 

    [self presentViewController:picker animated:YES completion:nil]; 
    //[[self navigationController]pushViewController:picker animated:YES]; 
    [searchLabel becomeFirstResponder]; 


} 


-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [searchBar resignFirstResponder]; 

} 

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar 
{ 
    searchBar.text = nil; 
    [searchBar resignFirstResponder]; 

} 


@end 

回答

2

我想出了我自己的问题。对于任何绊倒这个问题的人,我使用NSPredicate。我认为,如果有一个SQLite数据库,那么你不能使用NSPredicate,但我做到了。只要数据在一个数组中。