0

This app is a table view with a tab bar controller. I am logging the count of the array: arrayOfFavourites and even though i add an object is continues to have a nil value, my relating code, all objects shown are allocated and initialized in the code (previous or present) some are instances and some are properties:

ListViewController.m:

  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"TOUCHED CELL!");

// Push the web view controller onto the navigation stack - this implicitly 
// creates the web view controller's view the first time through
[[self navigationController] pushViewController:webViewController animated:YES];

// Grab the selected item
entry = [[channel items] objectAtIndex:[indexPath row]];

if (!entry) {
    NSLog(@"!entry");
}

// Construct a URL with the link string of the item
NSURL *url = [NSURL URLWithString:[entry link]];

// Construct a request object with that URL
NSURLRequest *req = [NSURLRequest requestWithURL:url];

  // Load the request into the web view 
[[webViewController webView] loadRequest:req];

// Take the cell we pressed
// IMPORTANT PART
CELL = [tableView cellForRowAtIndexPath:indexPath];

[webViewController setItem:entry];

webViewController = nil;
webViewController = [[WebViewController alloc] init];
[entry release];

  }

WebViewController.m:

You shake to favorite a cell

 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {

cellToPassOn = nil;

NSLog(@"Favouriting"); // YES I KNOW SPELLING

// This is pretty simple, what we do is we take the cell we touched and take its title and link 
// then put it inside an array in the Favourites class

Favourites *fav = [[Favourites alloc] init];
ListViewController *list = [[ListViewController alloc] init];
[self setCellToPassOn: [list CELL]];

if (!item) {
    NSLog(@"NILLED ITEM");

}

[[fav arrayOfFavourites] addObject:[item autorelease]];
[fav setCell: cellToPassOn];
[fav release];
[list release];
item = nil;

 }

Favourites.m:

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  {

arrayOfFavourites = [[NSMutableArray alloc] init];


NSLog(@"ROWS NO.");
NSLog(@"%i", [arrayOfFavourites count]);

return [arrayOfFavourites count];
}

4 Answers 4

2

Why are you inializing the array in tableview:numberOfRowsInSection ? This will cause the array to be reset each time table view is reloaded. This could be your issue.

Sign up to request clarification or add additional context in comments.

7 Comments

This could be also another issue, about memory leaks.
i put the allocation and initialization in the initWithStyle: method but is still doesn't work... I ANALYZED THERE ARE NO MEMORY LEAKS!
When is motionBegan:withEvent: invoked? After table reload? Move the initialization of the array to view did load, and reload the table after u add object to the array
arrayOfFavourites = [[NSMutableArray alloc] init]; Assigning an iVar in the above manner, is a clear case if memory leak.
@ShantiKamichetty: Only if the array is not released. The assignment alone is not evidence of a leak. (And under ARC, it will be released; an explicit release message is not necessary or valid.)
|
0

you are allocating your array in -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

try to allocate it somewhere else.

1 Comment

i put the allocation and initialization in the initWithStyle: method but is still doesn't work
0

You could allocate the arrayOfFavorites in the tableView:numberOfRowsInSectionMethod, but then you first need to check if it is nil.

if( !arrayOfFavorites )
    arrayOfFavoriges = [[NSMutableArray alloc] init];

You should release it then in the dealloc method: [arrayOfFavorites release].

3 Comments

i put that in the initWithStyle method, i logged in the numberOfRowsInSection: if it was nil and it was!
ok even weirder...... what i did was log the count in the numberOfRows.... section.... in my other class when i add the object to arrayOfFavourites i also reload the favorite class' table view. So i shook the device, so it gets logged again and the result turns to one as the count.... GOOD! I also log it when the view loads, when the view loads it gets logged as 0... WEIRD!!!!! I don't know why it goes nil when i enter its view.... do you know why???
also if i try shake twice in different articles (WEBVIEWCONTROLLER, that is a website, the webView changes for each cell) the array is still one then goes nil! WEIRD AGAIN! THEN........ the app crashes...
0
 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 cellToPassOn = nil;
 NSLog(@"Favouriting"); // YES I KNOW SPELLING
 // HERE creation of a Brand NEW empty Favourites instance
 Favourites *fav = [[Favourites alloc] init];
 // HERE creation of a Brand NEW empty ListViewController instance
 ListViewController *list = [[ListViewController alloc] init];
 // HERE we hope that the ListViewController as CELL other then nil when it is Brand NEW
 [self setCellToPassOn: [list CELL]];

 if (!item) {
     NSLog(@"NILLED ITEM");
 }

 [[fav arrayOfFavourites] addObject:[item autorelease]];
 [fav setCell: cellToPassOn];
 [fav release];
 // HERE the fav instance get deallocated and don't exist anymore
 [list release];
 // HERE the list instance get deallocated and don't exist anymore
 item = nil;
 }

In this code list and fav exist only in the body of this method, attempt to get to the value they have hold done to will failed, because list and fav doesn't exist outside that method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.