Iphone 使用uitable查看它的方式';是有意的
大家好 我几乎是第一次进入Objective-C。这是在踢我的屁股,但是(嘿)我想我终于开始“理解”一些概念了 我正在编写一个iPhone应用程序,它连接到web服务,以XML格式提取数据,解析数据,并将其呈现为UITableView。单击tableview中的单元格可加载详图视图,依此类推 我遇到了一些概念上的问题。在C#中,大多数控件都有一个显示标签和一个值。这样就可以很容易地将事件(另一件我上瘾的事情)附加到单元格或条目上,并关闭值以进行进一步查找 我遇到的问题是,虽然UITableView是为详细视图构建的,但它似乎没有值属性或任何我可以智能地键入以将标识符传递给详细视图并填充它的内容 目前,我通过将XML提要解析到一个NSMutableArray中来处理这个问题,该数组保存关于特定条目的所有数据。我基于该数组中的一个特定字段填充UITableView。然后我使用objectAtIndex:indexPath.row将任何标识符等传递给细节视图 我可以继续这样做,让它工作,但整个事情感觉像一个大烂摊子,好像它将是一个怪物,以维持/扩大。所以,既然这么说……请帮帮我!我将发布我的许多代码,这样您就可以看到我在这方面做得有多么糟糕:) 表视图控制器我问得太多了:Iphone 使用uitable查看它的方式';是有意的,iphone,objective-c,cocoa-touch,uitableview,ios4,Iphone,Objective C,Cocoa Touch,Uitableview,Ios4,大家好 我几乎是第一次进入Objective-C。这是在踢我的屁股,但是(嘿)我想我终于开始“理解”一些概念了 我正在编写一个iPhone应用程序,它连接到web服务,以XML格式提取数据,解析数据,并将其呈现为UITableView。单击tableview中的单元格可加载详图视图,依此类推 我遇到了一些概念上的问题。在C#中,大多数控件都有一个显示标签和一个值。这样就可以很容易地将事件(另一件我上瘾的事情)附加到单元格或条目上,并关闭值以进行进一步查找 我遇到的问题是,虽然UITableVie
#import "InvoicesTableViewController.h"
#import "Wrapper.h"
#import "InvoiceDetailViewController.h"
@implementation InvoicesTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[listOfItems release];
[restAPI release];
[parameters release];
[super dealloc];
}
- (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 - Wrapper
- (void)wrapper:(Wrapper *)wrapper didRetrieveData:(NSData *)data
{
NSData *result = [[restAPI responseAsText] dataUsingEncoding:NSUTF8StringEncoding];
if (result != nil)
{
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:result] autorelease];
parser.delegate = self;
[parser parse];
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
#pragma mark - Parser
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if(OneInvoice == nil)
{
OneInvoice = [[NSMutableArray alloc] init];
}
if ([elementName isEqualToString:@"invoice"])
{
[OneInvoice addObject:[attributeDict valueForKey:@"uri"]];
[OneInvoice addObject:[attributeDict valueForKey:@"total"]];
[OneInvoice addObject:[attributeDict valueForKey:@"total_due"]];
[OneInvoice addObject:[attributeDict valueForKey:@"status"]];
}
if ([elementName isEqualToString:@"client"])
{
[OneInvoice addObject:[attributeDict valueForKey:@"name"]];
[listOfItems addObject:[[NSMutableArray alloc] initWithArray:OneInvoice copyItems:YES]];
[OneInvoice release];
OneInvoice = nil;
[self.tableView reloadData];
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
listOfItems = [[NSMutableArray alloc] init];
//Set the title
self.navigationItem.title = @"Invoices";
if(restAPI == nil) {
restAPI = [[Wrapper alloc] init];
}
restAPI.delegate = self;
parameters = nil;
restAPI.mimeType = @"application/vnd.site+xml";
url = [NSURL URLWithString: @"https://user:pass@site.com/invoices/?status=all"];
[restAPI sendRequestTo:url usingVerb: @"GET" withParameters: parameters];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (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 [listOfItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Setup the cell
NSString *cellValue = [NSString stringWithFormat:@"%@ - %@", [[listOfItems objectAtIndex:indexPath.row] objectAtIndex:4],[[listOfItems objectAtIndex:indexPath.row] objectAtIndex:1]];
cell.textLabel.text = cellValue;
return cell;
}
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath {
//return UITableViewCellAccessoryDetailDisclosureButton;
return UITableViewCellAccessoryDetailDisclosureButton;
//return UITableViewCellAccessoryDisclosureIndicator;
}
/*
// 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;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *invoiceURL = [[listOfItems objectAtIndex:indexPath.row] objectAtIndex:0];
InvoiceDetailViewController *dvController = [[InvoiceDetailViewController alloc] initWithNibName:@"InvoiceDetailViewController" bundle:[NSBundle mainBundle]];
dvController.invoiceURL = invoiceURL;
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
dvController = nil;
}
@end
请不要犹豫嘲笑我的表格。我对C/C++非常缺乏经验,尤其是Objective-C。我昨晚一瘸一拐地做了这件事,只是为了让我可以建立的东西工作起来
万分感谢,
CliftonCocoa使用模型-视图-控制器范例。在您的案例中,模型是NSMUTABLEARRY、控制器、表视图控制器和视图UITableView。
表格单元格不包含任何数据。单击单元格时,控制器将收到通知,并将计算出相应的数据(就像您所做的那样),然后调用另一个控制器,更新模型或更新视图。您的代码看起来正确。我不明白你在担心什么“一团糟”。如果有数据源,则该数据源将显示在表中。用户从表中选择一行时,将数据引用传递给详图视图。如果你想要超级数据建模的功夫,你可以查看核心数据,你可以连接一个表视图来自动显示核心数据实体。欢迎来到Objective C系列,Clifton!我不是真的理解你的问题,你能用一个更直截了当的问题来澄清你的问题吗?你试过词典吗?这是一个关键的价值交易。更好的是,您可以创建一个存储各种数据的自定义对象。谢谢你的建议,谢谢你。在所有的回答中,我没有想到“你的代码看起来是正确的。”:)我已经习惯了使用数据集在C#中抽象这些东西,以至于我觉得我是在一起破解它。很少有微小的NIT,您只需将“nil”传递给initWithNibName:bundle:的bundle参数,并且不需要“dvController=nil;”,但将数据元素传递到行选择中的细节视图的要点是这非常有用。我将尝试学习NSDictionary,将其作为一种比数组更灵活的数据处理方式。