【发布时间】: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 "]){