在TableViewController中添加和删除列的最佳方法是什么?

时间:2012-02-21 17:45:18

标签: iphone ios ios5 ios4

下午好,我有问题,希望你能告诉我。首先,如果有人提出这样的问题我会道歉,我想不会,但仍然会原谅新手的错误

我正在做的项目是一个带有两个控制器的TabBarViewController。 一个基本上能够捕获条形码并使用条形码调用Web服务来从服务器获取项目。那个项目然后想要在另一个控制器上显示。

我的问题是我不知道如何将检索到的项目传递给我的自定义UITableViewController,或者这是实现此目的的最佳方法。

这是能够捕获条形码并连接到Web服务的界面

#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"
#import "ListaItemsViewController.h"


@interface ViewController : UIViewController < ZBarReaderDelegate,NSXMLParserDelegate >
{
    IBOutlet UILabel                  * resultText;
    ListaItemsViewController          * listaItemsViewController;

    MBProgressHUD                     * HUD;
    NSMutableData                     * xmlData;
   //neccesary to parse the possible error
   NSMutableString                   * faultString;
   BOOL                                esperandoFaultString;
   //neccesary to parse message from logIn and logOut methods webservice
   BOOL                                esperandoReturn;
   NSMutableString                   * returnString;
   //neccesary to parse and save an item
  BOOL                                esperandoItem;
  BOOL                                esperandoDescripcionItem;
  BOOL                                esperandoPrecioItem;
  BOOL                                esperandoNumTotalItem;
  NSMutableString                   * descripcionItem;
  NSMutableString                   * precioItem;
  NSMutableString                   * numeroTotalItem;

  NSXMLParser                       * parser;

}

@property (nonatomic,strong) MBProgressHUD            * HUD;
@property (nonatomic, retain) IBOutlet UILabel      * resultText;
@property(nonatomic,strong) NSMutableData           * xmlData;
@property(nonatomic,strong) NSMutableString         * faultString;
@property(nonatomic,strong) NSMutableString         * returnString;
@property(nonatomic,strong) NSMutableString         * descripcionItem;
@property(nonatomic,strong) NSMutableString         * precioItem;
@property(nonatomic,strong) NSMutableString         * numeroTotalItem;
@property(nonatomic,strong) NSXMLParser             * parser;
@property(nonatomic,strong) ListaItemsViewController          * listaItemsViewController;
- (IBAction) scanButtonTapped;

- (IBAction)esconderTeclado:(id)sender;
- (IBAction)mostrarTeclado:(id)sender;

@end

这是界面

 #import <UIKit/UIKit.h>

 @interface ListaItemsViewController : UITableViewController
 {
      // the item list
      NSMutableArray * listaItems;
 }

 @property(nonatomic,strong) NSMutableArray * listaItems;
 @end

这是实施文件:

  #import "ListaItemsViewController.h"
  #import "CaracteristicasItemViewController.h"

  @implementation ListaItemsViewController

  @synthesize listaItems;

  - (id)initWithStyle:(UITableViewStyle)style
  {
     NSLog(@"ListaItemsViewController. initWithStyle...");
     self = [super initWithStyle:style];
     if (self) {
        // Custom initialization
     }
     return self;
  }

  - (void)didReceiveMemoryWarning
  {
     // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
  }

  #pragma mark - View lifecycle

  - (void)viewDidLoad
  {
      NSLog(@"ListaItemsViewController. viewDidLoad...");
     **//how do i create this item list with the items passed via web service?**
     listaItems = [[NSMutableArray alloc] initWithObjects:@"item1",@"item2",@"item3", nil];
     [super viewDidLoad];
     self.navigationItem.rightBarButtonItem = self.editButtonItem; 
  }

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

  - (void)viewWillAppear:(BOOL)animated
  {
      [super viewWillAppear:animated];
  }

  - (void)viewDidAppear:(BOOL)animated
  {
     [super viewDidAppear:animated];
  } 

  - (void)viewWillDisappear:(BOOL)animated
  {
     [super viewWillDisappear:animated];
  }

  - (void)viewDidDisappear:(BOOL)animated
  {
    [super viewDidDisappear:animated];
  }

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
       // Return YES for supported orientations
       return (interfaceOrientation == UIInterfaceOrientationPortrait);
  }

  #pragma mark - Table view data source

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

  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {
        // Return the number of rows in the section.
        NSLog(@"[listaItems count]: %d",[listaItems count]);
       return [listaItems count];
  }

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
     static NSString *CellIdentifier = @"celda";

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

    // Configure the cell...
    cell.textLabel.text = [listaItems objectAtIndex:[indexPath row]];
    cell.detailTextLabel.text = [listaItems objectAtIndex:[indexPath row]];

    NSLog(@"cell: %@",cell.textLabel.text);
    return cell;
  }


  // Override to support conditional editing of the table view.
  - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  {
       // Return NO if you do not want the specified item to be editable.
       return YES;
  }



  // Override to support editing the table view.
  - (void)tableView:(UITableView *)tableView commitEditingStyle:   (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  {
   **when i try to delete some row, the app crash, check!!**
       NSLog(@"commitEditingStyle...");
       [tableView beginUpdates];
       if (editingStyle == UITableViewCellEditingStyleDelete) 
       {
         // Delete the row from the data source
          [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
          //[listaItems delete:[NSArray arrayWithObject:indexPath]];
       }   
       [tableView endUpdates];
       NSLog(@"end commitEditingStyle...");

    }




     #pragma mark - Table view delegate

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
     {
      // Navigation logic may go here. Create and push another view controller.

      NSLog(@"didSelectRowAtIndexPath indexPath.row: %d",indexPath.row);

             CaracteristicasItemViewController *caracteristicas =   [[CaracteristicasItemViewController alloc]   initWithNibName:@"CaracteristicasItemViewController" bundle:Nil];
            [self.navigationController pushViewController:caracteristicas animated:YES];

      }

@end

实现这一目标的最佳方式是最佳做法?

再次,对不起,如果这对你们来说太容易了,但我刚开始使用这项技术。 此致

3 个答案:

答案 0 :(得分:0)

如果您只想删除没有动画的行,您只需从数据源数组listaItems中删除该条目,然后在表格上执行-reloadData

答案 1 :(得分:0)

您还可以查看UITableView方法:

- (void)deleteRowsAtIndexPaths: (NSIndexPath) indexPath withRowAnimation: (UITableViewRowAnimation)animation;

答案 2 :(得分:0)

从我收集的内容和ViewController.h文件中的给定信息,您的ViewController有一个名为listaItemsViewController的ListaItemsViewController的瞬间。 ListaItemsViewController中有一个名为listaItems的iVar,因此您可以将检索到的项目分配给listaItems(在您实例化listaItemsViewController即时之后:

listaItemsViewController.listaItems = retrieveItems;  // or self.retrieveItems where retrievedItems is the array of items that you have retrieved from the web server.

不幸的是,我无法弄清楚你使用哪个变量。