XML值未传递给iPhone的detalview [英] xml values are not passed to detalview for iphone

查看:31
本文介绍了XML值未传递给iPhone的detalview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以某种方式不传输其数据,因此我正在使用其他显示方法,但这对内存管理有好处吗?

Somehow its data is not transferring so I am using other method for displaying but is this good for memory management ??

if ( [elementName isEqualToString:@"telnumber"]) {

   NSLog(@"Processing Value: %@", currentElementValue);
   NSUserDefaults *tel = [NSUserDefaults standardUserDefaults];
   [tel setObject:currentElementValue forKey:@"keyTotel"];
   return;
}

然后详细查看我正在使用的

and then in detail view i am using this

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"String = %@",aBook.telnumber);
    NSUserDefaults *get = [NSUserDefaults standardUserDefaults];
    NSString *mytel = [get stringForKey:@"keyTotel"];
    NSLog(@" LOCAL phone   is %@",mytel); 
}

那么对于XML上的所有6个元素,我必须遵循相同的过程就足够了吗?

So for all 6 elements on XML I have to follow same process is that good enough?

推荐答案

NSUserDefaults类提供了用于与默认系统进行交互的编程接口.默认系统允许应用程序自定义其行为以匹配用户的首选项.例如,您可以允许用户确定应用程序显示的度量单位或自动保存文档的频率.应用程序通过将值分配给用户默认数据库中的一组参数来记录此类首选项.这些参数被称为默认值,因为它们通常用于确定应用程序在启动时的默认状态或默认情况下的行为方式

The NSUserDefaults class provides a programmatic interface for interacting with the defaults system. The defaults system allows an application to customize its behavior to match a user’s preferences. For example, you can allow users to determine what units of measurement your application displays or how often documents are automatically saved. Applications record such preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults since they’re commonly used to determine an application’s default state at startup or the way it acts by default

因此,您不应真正使用 NSUserDefaults 来保存默认应用程序设置以外的任何内容(当然也不能保存临时变量).

So, you shouldn't really be using NSUserDefaults to hold anything other than default application settings (and certainly not for holding temporary variables).

我假设您是在 UITableView 出现之前在应用程序中的某个时刻解析XML文件的?

I assume you are parsing your XML file at some point in the application, prior to the UITableView appearing?

更好(简单)的方法是拥有另一个类,该类表示XML中正在描述的对象,然后用从XML中提取的数据填充该对象.(或在xml中为n个条目填充n个对象).

A better (simple) approach would be to have another class that represents the object being described in the XML, and populating this object with the data extracted from the XML. (or populate n objects for n entries in the xml).

因此,如果您的XML描述了您应用程序中的联系人: HumanContact.h

So, if your XML describes a contact in your app: HumanContact.h

@interface HumanContact : NSObject {
NSString* firstName;
NSString* surname;
NSString* telephoneNumber;
//etc...

}

@property (nonatomic, retain) NSString* firstName;
@property (nonatomic, retain) NSString* surname;
@property (nonatomic, retain) NSString* telephoneNumber;

HumanContact.m

#import "HumanContact.h"

@implementation HumanContact {

@synthesize firstName, surname, telephoneNumber;

// all default init + dealloc methods etc (remember to release the properties etc)
}

现在,此对象作为一种包含有关联系人的所有数据的方法而存在.它是您的模型.> MVC

Now this object exists as a method of containing all data about a contact. It is the Model in your MVC

在外部,当您解析XML时,无需为 NSUserDefaults 中的每个项目添加密钥,而是为XML中的每个记录创建一个 HumanContact 对象:

Externally, when you parse your XML, instead of adding a key for each item in NSUserDefaults, for each record in the XML you would create a HumanContact object:

HumanContact *contact = [[HumanContact init] alloc];

并适当地填充成员变量:

and populate the member variables appropriately:

if ( [elementName isEqualToString:@"telnumber"]) {

contact.firstName = currentElementValue;

}
//  etc for remaining attributes

然后可以将此联系人添加到成员变量(可以是单个 HumanContact 或它们的数组(更有可能)).

This contact can then be added to a member variable (could be a single HumanContact or an array of them (more likely)).

一旦解析了XML,就可以使用该信息的本地表示形式,该信息可以在类中的任何位置进行访问(并非常简单地传递给其他类).

Once you've parsed the XML you have a local representation of this information that can be accessed anywhere in the class (and passed to other classes very simply).

在您的(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath 中,您只需查询模型中的信息即可,而不是 NSUserDefaults

In your (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath you simply query your model for the information, instead of NSUserDefaults

(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HumanContact *curContact = [arrayOfContacts getItemAtIndex: indexPath.row];
cel.text = curContact.telephoneNumber;
//...
}

希望这会有所帮助

修改

在tableViewController中(无论您在哪里解析xml),都需要在.h中添加一个数组

In your tableViewController (wherever you are parsing the xml) you need to have an array in the .h

@interface FooTableViewController
{
  NSMutableArray *books;
}

@property (nonatomic, retain) NSMutableArray *books;

然后在.m中:

@synthesize books;

viewDidLoad

self.books = [[[NSMutableArray alloc] init] autorelease];

解析xml的位置:

book.telNumber = currentElement;
[self.books addItem:book]; // array is of books, so add the whole thing

然后在tableViewCell中图书currentBook = [this.books objectAtIndex:indexPath.row];NSString * telNum = currentBook.telNumber;

Then in your tableViewCell Book currentBook = [this.books objectAtIndex: indexPath.row]; NSString *telNum = currentBook.telNumber;

请记住,您要将 Book 添加到数组中,并从该数组中重新整理一本书.

Remember that you are adding the Book to the array, and you ger a book back from the array.

这篇关于XML值未传递给iPhone的detalview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆