【问题标题】:what is the best way to add and delete columns in a TableViewController?在 TableViewController 中添加和删除列的最佳方法是什么?
【发布时间】:2012-02-21 17:45:17
【问题描述】:

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

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

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

这是一个能够捕获条形码并连接到网络服务的界面

#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

实现这一目标的最佳方式是什么,最佳实践是什么?

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

【问题讨论】:

  • 题目和你的实际题目不一样?哪个是正确的?
  • 感谢您的回复,并为给您带来的不便深表歉意。我的问题是我不知道如何将检索到的项目传递给我的自定义 UITableViewController,或者这是实现此目的的最佳方法。使用第一个控制器检索项目,我想在自定义 tableview 上添加项目。
  • 我记下了我认为是你的问题。但是现在我不确定您是一次传递一个项目还是一组项目?
  • 现在我意识到我需要一次传递一个项目。我试过这种方式:
  • - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName { if ([elementName isEqual:@"item "]){

标签: iphone ios ios5 ios4


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    你也可以看看 UITableView 方法:

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

    【讨论】:

      【解决方案3】:

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

      很遗憾,我无法弄清楚您使用了哪些变量。

      【讨论】:

      • 您好,谢谢您的回复!。你几乎想通了一切。我的 ViewController 有一个名为 listaItemsViewController 的 ListaItemsViewController,里面有一个名为 listaItems 的 NSMutableArray。这就是我想要做的:
      • 我试过你的方法,但它不起作用。很抱歉给您带来不便。/**NSMutableArray * lista = [[NSMutableArray alloc] initWithObjects:@"itemcito1",@"itemcito2", nil]; if (!listaItemsViewController) listaItemsViewController = [[ListaItemsViewController alloc] init]; listaItemsViewController.listaItems = lista; //ahora muestro este mensaje proveniente del backend NSLog(@"add items to listaItems..."); **/
      • 这是 ListaItemsViewController.viewDidLoad 方法: /**- (void)viewDidLoad { NSLog(@"ListaItemsViewController.viewDidLoad..."); [超级视图DidLoad]; // 取消注释以下行以在此视图控制器的导航栏中显示一个编辑按钮。 self.navigationItem.rightBarButtonItem = self.editButtonItem; } **/
      • 这是 ListaItemsViewController.numberOfRowsInSection 方法: /** - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // 返回节中的行数。 NSLog(@"[listaItems count]: %d",[listaItems count]);返回 [listaItems 计数]; } **/ 来自日志的消息是 ListaItemsViewController 旁边的“将项目添加到 listaItems ...”。 viewDidLoad... 最后 [listaItems count]: 0 怎么可能,做错了什么?
      • 抱歉缩进不佳
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-15
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多