How to add data to a TableView? - iphone

I've got a tableView in a view which is a subview to my MainViewController. When the subview is loaded, I have this in my viewDidLoad method to initialize my tableData:
NSMutableArray *array = [[NSMutableArray alloc] init];
self.listData = array;
[array release];
[super viewDidLoad];
Then I use other methods to add to this data within this view.
When I switch back to my other view, however, then bring up this view again, it initializes the data all over again, erasing my changes. How can I fix this problem?
EDIT:
I tried to initialize this array in MainViewController by doing this:
NSMutableArray *array = [[NSMutableArray alloc] init];
HistoryViewController.listData = array;
[array release];
But it says Accessing unknown 'setListData:' class method.

you should not be initializing your array in view did load
// this is clearing out all of your data
NSMutableArray *array = [[NSMutableArray alloc] init];
self.listData = array;
There is not much code, but I would suggest initializing the listData property when you initialize the ViewController.
I might suggest you review this tutorial here on UITableView, and your code above should be edited
HistoryViewController.listData = array;
should be
self.listData = array;

Related

Select an array from an array of arrays to display in uitableview (iphone)

I have an array called addArray which I am adding array objects to:
NSMutableArray *addArray = [NSMutableArray array];
[addArray addObjectsFromArray: delegate.arrayobjectOne];
[addArray addObjectsFromArray: delegate.arrayobjectTwo];
// etc...
Now, if I only want to init one of these arrays to display in my table (preferably from another view controller but that's another question), how would I do this? And how would I access a specific property of each array object, e.g. arrayobjectOne.info?
Thanks for your time.
you can say
someObject = [addArray objectAtIndex: someIndex];
use this Example
NSMutableArray *objectsToAdd= [[NSMutableArray alloc] initWithObjects:#"one",#"two", nil];
NSMutableArray *myArray = [[NSMutableArray alloc] initWithObjects:objectsToAdd,nil];
self.list = [[NSMutableArray alloc] init];
[self.list addObjectsFromArray:myArray];

NSMutableArray not accessible throughout my class?

I'm really frustrated after spending like three hours googling around to solve this problem!! It's probably an easy solution to it aswell.
I'm creating a really simple TableView app for the iPhone. It's supposed to fetch an XML-document and parse it (already fixed) and then put the data into objects called HPobject!
One HPobject represents one day of data from the XML-file. Anyhow!
Then I want the object to be stored in a NSMutableArray so I can grab it later when I'm creating the table rows. But I can't access it! My NSMutableArray is ALWAYS null! No matter what I do!
Here's my code:
//THE .h FILE
#import "TBXML.h"
#import "HPobject.h"
#interface RootViewController : UITableViewController {
NSMutableArray *listOfItems;
}
- (void)traverseElement:(TBXMLElement *)element;
//THE .m FILE
#import "RootViewController.h"
#import "HPobject.h"
#implementation RootViewController
- (void)viewDidLoad
{
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
TBXML * tbxml = [[TBXML tbxmlWithURL:[NSURL URLWithString:#"http://www.hpprov.se/istasts.php?q=xxxxxxx"]] retain];
if (tbxml.rootXMLElement)
[self traverseElement:tbxml.rootXMLElement]; //Works fine!
[tbxml release];
[super viewDidLoad];
}
- (void) traverseElement:(TBXMLElement *)element {
do {
//Lots of parsing code which all works fine and gets me the variables up next!
HPobject *currentObject = [[HPobject alloc] init];
currentObject.antalRegistrerade = numRegistrerade;
currentObject.inkomstBrutto = numBrutto;
currentObject.inkomstNetto = numNetto;
[listOfItems addObject:currentObject];
NSLog(#"Array: %#", listOfItems); //RETURNS null!
NSLog(#"Reg: %#, Net: %#, Brutt: %#", currentObject.antalRegistrerade, currentObject.inkomstNetto, currentObject.inkomstBrutto); //Returns the correct values!
NSLog(#"%d stycken!", listOfItems.count); //Returns 0!! :(
[currentObject release];
} while ((element = element->nextSibling));
}
You are defining listOfItems locally in viewDidLoad and then you try to access that in another method.
Make sure you are using an instance variable defined in your interface definition (header).
replace this line in viewDidLoad:
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
with
listOfItems = [[NSMutableArray alloc] init];
I suppose that listOfItems is an ivar. So to fix your problem change this:
NSMutableArray *listOfItems = [[NSMutableArray alloc] init];
to this
listOfItems = [[NSMutableArray alloc] init];
Take a look at the scope of your array. You have created that as a variable of another method. It will not visible in others. Make an instance var.

iPhone addObject to NSMutableArray from another ViewController Using IBAction

I am trying to addObject to a NSMutableArray once the user taps the Add to Favorites button I can get the data into a NSDictionary, but when I pass the NSDictionary to the Array the NSLog comes back with nil. Am I missing something?
-(IBAction) addtofavorites: (id)sender
{
NSArray *key = [NSArray arrayWithObjects:#"Title", #"Description", nil];
NSArray *objects = [NSArray arrayWithObjects:CurrentTitle, description.text, nil];
NSDictionary *fadd = [[NSDictionary alloc] initWithObjects:objects forKeys:key];
FavoritesViewController *fvc = [[FavoritesViewController alloc] init];
[fvc.favorites addObject:fadd];
[FavoritesViewController release];
}
What I would do is in your header file create an instance variable for your FavoritesViewController that you retain. Then use lazy init when the addtofavorites is pressed. Something like below
if (!detailViewController)
{
self.fvc = [[FavoritesViewController alloc] init];
}
[self.fvc.favorites addObject:fad];
Then just release the FavoritesViewController object in dealloc
- (void)dealloc {
[fvc release];
[super dealloc];
}
FavoritesViewController *fvc = [[FavoritesViewController alloc] init];
[fvc.favorites addObject:fadd];
[FavoritesViewController release];
doesn't look right. You should already have a FavoritesViewController initialized (in which can just access it and call -addObject:)...
EDIT
First, how does the current view controller (the one that has -addtofavorites: defined on it) relate to FavoritesViewController? How would the user navigate between these two view controllers?
Second, would the list of favorites persist across different runs of the app? If so, how do you plan to save/restore favorites?
Third, rather than add favorites to a view controller, you would probably do better to add them to an underlying 'model' that would be used to populate the FavoritesViewController.
Separating the 'model' from the 'view' can be very powerful, while also simplifying your code.

memory management question -- releasing an object which has to be returned

I have an NSMutableArray called playlist. This is in a method called getAllPlaylists. The code is something like this:
-(NSMutableArray *)getAllPlaylists
{
//playlist is an instance variable
playlist = [[NSMutableArray alloc] init]; //memory leak here
...
//some code here which populates the playlist array
[playlist addObject: object1];
...
return playlist;
}
The array allocation step of playlist is causing a memory leak. In such a scenario where can i release this array? Or can i avoid allocation n initialization of playlist here by doing something else? Any help will be greatly appreciated!!
2 solutions:
Use autorelease:
- (NSMutableArray*)getAllPlaylists
{
playlist = [[NSMutableArray alloc] init];
...
return [playlist autorelease];
}
or instead of using [[NSMutableArray alloc] init] to create your NSMutableArray object, use [NSMutableArray array] which is equivalent to [[[NSMutableArray alloc] init] autorelease]:
- (NSMutableArray*)getAllPlaylists
{
playlist = [NSMutableArray array];
...
return playlist;
}
You should autorelease newly created objects that you want to return that are not owned by the object (local variables, not instance variables).
playlist = [[[NSMutableArray alloc] init] autorelease];
Alternatively, you can use the convenience method to do that more easily:
playlist = [NSMutableArray array];
For items the object owns (instance variables), you should make sure you release the old value first and implement a dealloc method that also releases the value.
- (NSMutableArray*)getAllPlaylists {
[playlist release];
playlist = [[NSMutableArray alloc] init];
return playlist;
}
- (void)dealloc {
[playlist release];
[super dealloc];
}
For more info, see the memory management guide.

Building an array of arrays

I am trying to get the following loop working to fill an array of arrays:
while (condition) {
[itemsArray fillFromDB];
if (! [checkArray containsObject:checkFlag]) {
// Add existing itemsArray to myArray
if (itemsArray.count) {
// add the itemsArray to myArray and create a new instance of itemsArray
[myArray addObject:itemsArray];
[itemsArray release];
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
}
[itemsArray addObject:myObject];
[checkArray addObject:checkFlag];
} else {
[itemsArray addObject:tmpEvent];
} }
However I try to shape this loop it falls over the release of itemsArray
when I use release (as above), the array does not re-initialise as a new instance with alloc. Whilst expecting emptyness, the next Object is added to the old array.
when I use removeAllObjects, my Array is emptied and so is the array attached to myArray.
Where am I going in the wrong direction?
You might place:
itemsArray = nil;
after the release message, to ensure that you're not pointing to an old instance.
EDIT
Looking at this again, you have:
NSMutableArray *itemsArray = [[NSMutableArray alloc] init];
This is scoped within the if statement.
So take out NSMutableArray and just use:
itemsArray = [[NSMutableArray alloc] init];
Don't write NSMutableArray *itemsArray = [[NSMutableArray alloc] init];--you're re-declaring the variable in the scope of the if statement, so outside the if statement, itemsArray will still refer to the old value (I'm not sure why the compiler isn't complaining). You can just say itemsArray = [[NSMutableArray alloc] init] instead.
You also might want to use autorelease, to simplify, as well.
The others have found the problem, but have created a new problem. The first time you create the mutable array, you need to use NSMutableArray *itemsArray = [[NSMutableArray alloc] init];. Then, after, you can release and use itemsArray = [[NSMutableArray alloc] init];. It is important that the first one (the one that creates the pointer) occurs only once, and the rest can occur as many times as desired.
EDIT:
You could write NSMutableArray *itemsArray; before the if statement, and then use itemsArray = [[NSMutableArray alloc] init]; in the if statement.