数组为零(即使是addObject :)

时间:2011-12-20 00:45:14

标签: iphone objective-c cocoa-touch nsmutablearray

  

可能重复:
  addObject: to array not working (array still nil)

一切都在更新

此应用是带有标签栏控制器的表格视图。我正在记录数组的计数:arrayOfFavourites,即使我添加一个对象继续有一个nil值,我的相关代码,显示的所有对象都在代码中分配和初始化(之前或现在)有些是实例,有些是属性:

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:

你摇动到最喜欢的细胞

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

Favourites.m:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"ROWS NO.");
    NSLog(@"%i", [arrayOfFavourites count]);

    return [arrayOfFavourites count];
}

收藏夹:

TAB BAR控制器上的CLASS

WEBVIEWCONROLLER:

不同网页视图的控制器

LISTVIEWCONTROLLER:

DATA PROVIDER

实际上发生的事情是当我摇动我重新加载表视图数据并且我将一个对象添加到一个数组(收藏夹数组)时,计数就是一个....好!但是当我再次摇晃时(在另一篇文章中,当我按下不同的单元格时,我的应用程序有不同的webView。)它仍然是1 ...很奇怪....如果我去收藏夹类,那个数组仍然是一个......好吧。 ..所以你可以看到我将arrayOfFavourites计数返回到numberOfRowsInSection(这是1)但没有出现单元格,并且从未调用过cellForRowAtIndexPath(使用NSLOG)为什么会发生这种情况我非常恼火!

3 个答案:

答案 0 :(得分:3)

在您的Favourites.m numberOfRowsInSection函数中,看起来应该这样做:

if(arrayOfFavourites == NULL)
{
    arrayOfFavourites = [[NSMutableArray alloc] init];
}

因为你在每次调用numberOfRowsInSection时都会重新初始化(并且可能会泄漏)(每次表需要知道它必须知道它必须显示多少行时才会被调用 - 即非常频繁)。

答案 1 :(得分:2)

每次浏览-motionBegan:withEvent:时都会创建并销毁新的“收藏夹”对象,并且每次浏览-tableView:numberOfRowsInSection:时都会创建一个新数组。如果您希望数据持续超出这些事件,则需要保留对象。

答案 2 :(得分:1)

让我们回顾一下您的代码部分:

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

除非我错误地阅读你的代码,否则我觉得你正在尝试使用“volatile”对象进行持久化。

您需要为这些对象设置@property才能在一次方法调用后存活更长时间 每次创建一个新对象时,它都是新的,并且不知道先前的对象。

如果我们看一下这段代码:

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