- Array nil (even when addObject:) [closed]
- -(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];
- }
- -(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];
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- NSLog(@"ROWS NO.");
- NSLog(@"%i", [arrayOfFavourites count]);
- return [arrayOfFavourites count];
- }
- if(arrayOfFavourites == NULL)
- {
- arrayOfFavourites = [[NSMutableArray alloc] init];
- }
- -(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
- // HERE you are creating a NEW fav
- Favourites *fav = [[Favourites alloc] init];
- // HERE you are creating a NEW list
- ListViewController *list = [[ListViewController alloc] init];
- // SO HERE what is "CELL" doing, returning some constant or static object?
- [self setCellToPassOn: [list CELL]];
- // HERE what is item and where does it come from
- if (!item) {
- NSLog(@"NILLED ITEM");
- }
- // Here you take an array of an object you just created and autoreleasing the item
- // this is not the regular way to handle memory management in Cocoa,
- // depending on what you are doing to item else where you could get item == deallocated pretty soon
- [[fav arrayOfFavourites] addObject:[item autorelease]];
- [fav setCell: cellToPassOn];
- HERE you are releasing fav
- [fav release];
- HERE fav don't exist anymore as well as the array to which you've added something to it.
- [list release];
- item = nil;
- }
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- // HERE everytime the tableview is asking for the numberOfRowsInSection, you create a new array
- // And that new empty array is replacing your old one.
- // That is your "weird" thing, it's doing what you are asking it to do, set it back to a new empty array.
- arrayOfFavourites = [[NSMutableArray alloc] init];
- NSLog(@"ROWS NO.");
- NSLog(@"%i", [arrayOfFavourites count]);
- return [arrayOfFavourites count];
- }