typedef enum addingMode {
NAME,
BARCODE,
SCAN,
} ADDING_MODE;
@interface ProductViewController : UITableViewController {
UITableView *productTableView;
NSString *productName, *productCategory, *productBarcode;
UIButton *productScan;//to scan a product
UITextField *nameTextField, *categoryTextField, *barcodeTextField;
NSMutableArray *listsTable1;
ADDING_MODE addMode;
UISegmentedControl *segmentedControl;
}
@property(nonatomic, retain) UITextField *nameTextField;
@property(nonatomic, retain) UITextField *categoryTextField;
@property(nonatomic, retain) UITextField *barcodeTextField;
@property(nonatomic, retain) NSString *productName;
@property(nonatomic, retain) NSString *productCategory;
@property(nonatomic, retain) NSString *productBarcode;
-(void) pickOne:(id)sender;
@end
#import "ProductViewController.h"
@implementation ProductViewController
@synthesize productName, productCategory, productBarcode;
@synthesize nameTextField, categoryTextField, barcodeTextField;
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
[super loadView];
float navBarHeight = self.navigationController.navigationBar.frame.size.height;
float toolbarHeight = self.navigationController.toolbar.frame.size.height;
CGRect tableFrame = CGRectMake(0, navBarHeight, 320, 480 - navBarHeight - toolbarHeight);
self.tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
self.tableView.delegate = self;
self.tableView.dataSource = self;
//Two text fields to be showw together in a section when clicking the button Name
nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
[nameTextField setDelegate:self];
[nameTextField setPlaceholder:@"Product name"];
categoryTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
[categoryTextField setDelegate:self];
[categoryTextField setReturnKeyType:UIReturnKeyDone];
[categoryTextField setPlaceholder:@"Category"];
//One text field to be shown when the button Barcode is shown
barcodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
[barcodeTextField setPlaceholder:@""];
[barcodeTextField setKeyboardType:UIKeyboardTypeNumberPad];
[barcodeTextField setDelegate:self];
//Segmented control
NSArray *itemArray = [NSArray arrayWithObjects:@"Name", @"Barcode", @"Scan", nil];
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(9, 0, 302, 50);
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];
addMode = NAME;
}
-(void) scanButtonPressed {
NSLog(@"scan button pressed");
//Showing vamera in ModalView
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField == nameTextField) {
[categoryTextField becomeFirstResponder];
}
if(textField == categoryTextField) {
[barcodeTextField becomeFirstResponder];
}
return YES;
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if(section == 0) {
return 1;
}
else if(section == 1){
if(addMode == NAME) {
return 2;
}
if(addMode == BARCODE) {
return 1;
}
}
return 0;
}
// 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:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
[cell setEditing:YES];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
if(indexPath.section == 0) {
[cell addSubview:segmentedControl];
[segmentedControl release];
}
else if(indexPath.section == 1) {
if(addMode == NAME) {
if(indexPath.row == 0) {
[cell addSubview:nameTextField];
}
else if(indexPath.row == 1) {
[cell addSubview:categoryTextField];
}
}
else if(addMode == BARCODE) {
[cell addSubview:barcodeTextField];
}
else if(addMode == SCAN){
//Scanning mode
}
}
return cell;
}
-(void) pickOne:(id)sender {
addMode = [segmentedControl selectedSegmentIndex];
NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 1)];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationRight];
[indexSet release];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// The header for the section is the region name -- get this from the region at the section index.
if(section == 0) {
return @"Add new product";
}
else {
if(addMode == NAME)
return @"Product information";
if(addMode == BARCODE)
return @"Enter a product barcode";
}
return @"";
}