Question

I'm new to programming. I am building a program that scans barcodes and puts the UPC number in the search box in my table view. Now all i want to do is create a search for the UPC scanned. the viewTable is displaying an inventory from a sqlite database. The search bar is built all i need to do is program it to search, and I have not found any good tutorials on how to do this. How can I implement this?

#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
Was it helpful?

Solution

I figured out my own question. for anyone who stumbles across this question, i used NSPredicate. i assumed that if there is a sqlite database then you cannot use NSPredicate, but i made it work. As long as the data is in an array..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top