tags:

views:

32

answers:

1

somehow its data is not transferring so i am using other method for dsiplaying 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);

}

so for all 6 elemtns on XML i have to follow same process is that good enough ???

+2  A: 

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.

davbryn
Seems my MVC link hasn't worked, so here is the intended link: http://en.wikipedia.org/wiki/Model–view–controller
davbryn
i think i am not able to add them to arrayif ( [elementName isEqualToString:@"telnumber"]) { NSLog(@"telphne is = %@",aBook.telnumber);// working perfect // how to add them to arry??? aBook.telnumber = currentElementValue; [appDelegate.books addObject:aBook.telnumber];}lets say i have abook as object name now i am using this in detail view- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"telphne is = %@",aBook.telnumber);/ Its NULL }
ram
in table cell i am displaying values form bothfield SQL and XML and aBook is storing all info
ram
lets say i am not adding to array then can i access that object by some index????if ( [elementName isEqualToString:@"telenumber"]) { aBook.telnumber = currentElementValue; return; }how to
ram
I've updated with an elaboration at the end of the post
davbryn
thanks now lets say in detail view i have 5 fields of which 1st 3 elements are called from SQL and last 2 elemnts are from xml (i am parsing that xml when user press any cell before going to detail view)so i am using this- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];but this book is having some value from SQl so what should i do as i cant append my XML value to book arrayso what i did is i made a new array bookBBook *aBook = [appDelegate.bookB objectAtIndex:indexPath.row];
ram
but either i can use Book *aBook = [appDelegate.bookB objectAtIndex:indexPath.row];or Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];but not both :(
ram
Can't you build the Book object with info from both SQL and XML?Either build the Book with the xml values and add it to the array. Then when you get the SQL data, find the Book in the array and add the new data. so you parse xml and end up with an array of books, but they are missing the SQL data. When you get the SQL data, get the book from the array and add the extra stuff.from XML book.telNumber = currentElement; [self.books addItem:book];then you get SQL Book *curBook = [books itemAtIndex:0]; existingBook.title = sqlTitle
davbryn
Thanks for your help but i am confused i wish my NSDefault will save me and will not make my app hang .
ram