addObject: to array not working (array still nil)

痴心易碎 提交于 2019-12-04 06:46:53

问题


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];
}

回答1:


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.




回答2:


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

try to allocate it somewhere else.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/8560898/addobject-to-array-not-working-array-still-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!