Advertisement
Guest User

StackOverflow 2543762 cells-order-changes-in-uitableview-after-reloadsections-method

a guest
Mar 30th, 2010
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     typedef enum addingMode {
  2.         NAME,
  3.         BARCODE,
  4.         SCAN,
  5.     } ADDING_MODE;
  6.  
  7.     @interface ProductViewController : UITableViewController {
  8.  
  9.         UITableView *productTableView;
  10.         NSString *productName, *productCategory, *productBarcode;
  11.         UIButton *productScan;//to scan a product
  12.         UITextField *nameTextField, *categoryTextField, *barcodeTextField;
  13.         NSMutableArray *listsTable1;
  14.         ADDING_MODE addMode;
  15.         UISegmentedControl *segmentedControl;
  16.     }
  17.  
  18.     @property(nonatomic, retain) UITextField *nameTextField;
  19.     @property(nonatomic, retain) UITextField *categoryTextField;
  20.     @property(nonatomic, retain) UITextField *barcodeTextField;
  21.     @property(nonatomic, retain) NSString *productName;
  22.     @property(nonatomic, retain) NSString *productCategory;
  23.     @property(nonatomic, retain) NSString *productBarcode;
  24.  
  25.     -(void) pickOne:(id)sender;
  26.  
  27.     @end
  28.  
  29.  
  30.  
  31.     #import "ProductViewController.h"
  32.  
  33.     @implementation ProductViewController
  34.  
  35.     @synthesize productName, productCategory, productBarcode;
  36.     @synthesize nameTextField, categoryTextField, barcodeTextField;
  37.  
  38.  
  39.     // Implement loadView to create a view hierarchy programmatically, without using a nib.
  40.     - (void)loadView {
  41.         [super loadView];
  42.  
  43.         float navBarHeight = self.navigationController.navigationBar.frame.size.height;
  44.         float toolbarHeight = self.navigationController.toolbar.frame.size.height;
  45.         CGRect tableFrame = CGRectMake(0, navBarHeight, 320, 480 - navBarHeight - toolbarHeight);
  46.         self.tableView  = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStyleGrouped];
  47.         self.tableView.delegate = self;
  48.         self.tableView.dataSource = self;
  49.  
  50.         //Two text fields to be showw together in a section when clicking the button Name
  51.         nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
  52.         [nameTextField setDelegate:self];
  53.         [nameTextField setPlaceholder:@"Product name"];
  54.  
  55.         categoryTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
  56.         [categoryTextField setDelegate:self];
  57.         [categoryTextField setReturnKeyType:UIReturnKeyDone];
  58.         [categoryTextField setPlaceholder:@"Category"];
  59.  
  60.         //One text field to be shown when the button Barcode is shown
  61.         barcodeTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, 300, 40)];
  62.         [barcodeTextField setPlaceholder:@""];
  63.         [barcodeTextField setKeyboardType:UIKeyboardTypeNumberPad];
  64.         [barcodeTextField setDelegate:self];
  65.  
  66.         //Segmented control
  67.         NSArray *itemArray = [NSArray arrayWithObjects:@"Name", @"Barcode", @"Scan", nil];
  68.         segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
  69.         segmentedControl.frame = CGRectMake(9, 0, 302, 50);
  70.         segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
  71.         segmentedControl.selectedSegmentIndex = 0;
  72.         [segmentedControl addTarget:self action:@selector(pickOne:) forControlEvents:UIControlEventValueChanged];
  73.         addMode = NAME;
  74.     }
  75.  
  76.     -(void) scanButtonPressed {
  77.         NSLog(@"scan button pressed");
  78.         //Showing vamera in ModalView
  79.     }
  80.  
  81.  
  82.     -(BOOL)textFieldShouldReturn:(UITextField *)textField {
  83.         if(textField == nameTextField) {
  84.             [categoryTextField becomeFirstResponder];
  85.         }
  86.         if(textField == categoryTextField) {
  87.             [barcodeTextField becomeFirstResponder];
  88.         }
  89.         return YES;
  90.     }
  91.  
  92.  
  93.     #pragma mark Table view methods
  94.  
  95.     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  96.         return 2;
  97.     }
  98.  
  99.  
  100.     // Customize the number of rows in the table view.
  101.     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  102.         if(section == 0) {
  103.             return 1;
  104.         }
  105.         else if(section == 1){
  106.             if(addMode == NAME) {
  107.                 return 2;
  108.             }
  109.             if(addMode == BARCODE) {
  110.                 return 1;
  111.             }
  112.         }
  113.         return 0;
  114.     }
  115.  
  116.  
  117.     // Customize the appearance of table view cells.
  118.     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  119.         static NSString *CellIdentifier = @"Cell";
  120.  
  121.  
  122.         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  123.         if (cell == nil) {
  124.             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
  125.             [cell setEditing:YES];
  126.             [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  127.         }
  128.  
  129.  
  130.  
  131.         if(indexPath.section == 0) {
  132.             [cell addSubview:segmentedControl];
  133.             [segmentedControl release];
  134.         }
  135.  
  136.         else if(indexPath.section == 1) {
  137.             if(addMode == NAME) {
  138.                 if(indexPath.row == 0) {
  139.                     [cell addSubview:nameTextField];
  140.                 }
  141.                 else if(indexPath.row == 1) {
  142.                     [cell addSubview:categoryTextField];
  143.                 }
  144.             }
  145.             else if(addMode == BARCODE) {
  146.                 [cell addSubview:barcodeTextField];
  147.             }
  148.             else if(addMode == SCAN){
  149.  
  150.                 //Scanning mode
  151.             }
  152.         }
  153.         return cell;
  154.     }
  155.  
  156.     -(void) pickOne:(id)sender {
  157.         addMode = [segmentedControl selectedSegmentIndex];
  158.         NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1, 1)];
  159.         [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationRight];
  160.         [indexSet release];
  161.     }
  162.  
  163.  
  164.  
  165.     // Override to support conditional editing of the table view.
  166.     - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  167.         return YES;
  168.     }
  169.  
  170.  
  171.     // Override to support rearranging the table view.
  172.     - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
  173.     }
  174.  
  175.  
  176.     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  177.         // The header for the section is the region name -- get this from the region at the section index.
  178.         if(section == 0) {
  179.             return @"Add new product";
  180.         }
  181.         else {
  182.             if(addMode == NAME)
  183.                 return @"Product information";
  184.             if(addMode == BARCODE)
  185.                 return @"Enter a product barcode";
  186.         }
  187.         return @"";
  188.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement