开发者

xml values are not passed to detalview for iphone

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);
    NSUserDe开发者_开发问答faults *get = [NSUserDefaults standardUserDefaults];
    NSString *mytel = [get stringForKey:@"keyTotel"];
    NSLog(@" LOCAL phone   is %@",mytel); 
}

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

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

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

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).

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)
}

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

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

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

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).

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

i.e

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

Hope this helps

edit

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;

Then in the .m:

@synthesize books;

in viewDidLoad

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

where you parse your xml:

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

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜