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

发布于 2025-01-07 22:20:40 字数 6955 浏览 1 评论 0原文

下午好,我有一个问题,希望你能给我建议。首先,如果已经有人问过类似的问题,我表示歉意,我认为没有,但仍然请原谅菜鸟的错误

我正在做的项目是一个带有两个控制器的 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

实现此目的的最佳方法是什么,最佳实践是什么?

再次,抱歉,如果这对你们来说太容易了,但我刚刚开始使用这项技术。 问候

Good afternoon, I have a problem and hope you can advise me. First of all I apologize if something like this has already been asked, I think not, but still, forgive rookie mistakes

The project I'm doing is a TabBarViewController with two controllers.
One that basically is able to capture a bar code and make a call to a Web service with the bar code to get me the item from the server. That item then want to show on the other controller.

My problem is i don't know how to pass the retrieved item to my custom UITableViewController, or which is the best way to achieve this.

This is the interface able to capture a bar code and connect to a web service

#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

and this is the interface

 #import <UIKit/UIKit.h>

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

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

this is the implementation file:

  #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

Whats the best way to achieve this, the best practice?

Again, sorry if this is too easy for you guys, but I just started with this technology.
Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

堇色安年 2025-01-14 22:20:41

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

If you just want to remove the row without animation you could just delete the entry from the data source array listaItems and do a -reloadData on the table.

欢你一世 2025-01-14 22:20:41

您还可以查看 UITableView 方法:

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

You can also look at the UITableView method:

- (void)deleteRowsAtIndexPaths: (NSIndexPath) indexPath withRowAnimation: (UITableViewRowAnimation)animation;
诗笺 2025-01-14 22:20:41

根据我收集的信息以及 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.

不幸的是,我无法弄清楚您使用了哪些变量。

From what I gather and with the given information from your ViewController.h file, your ViewController has an instant of ListaItemsViewController called listaItemsViewController. And there is an iVar called listaItems in the ListaItemsViewController, so you can assign the retrieved items to listaItems like this (after you have instantiated the listaItemsViewController instant:

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

Unfortunately, I could not figure out which of the variables you used for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文