【问题标题】:addObject: to array not working (array still nil)addObject:到数组不起作用(数组仍然为零)
【发布时间】:2011-12-19 11:50:58
【问题描述】:

这个应用是一个带有标签栏控制器的表格视图。我正在记录数组的计数: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];
[fav release];
[list release];
item = nil;

 }

Favorites.m:

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

arrayOfFavourites = [[NSMutableArray alloc] init];


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

return [arrayOfFavourites count];
}

【问题讨论】:

    标签: objective-c cocoa-touch uitableview nsmutablearray


    【解决方案1】:

    为什么要初始化 tableview:numberOfRowsInSection 中的数组?这将导致每次重新加载表视图时重置数组。这可能是你的问题。

    【讨论】:

    • 这也可能是另一个问题,关于内存泄漏。
    • 我将分配和初始化放在 initWithStyle: 方法中,但仍然不起作用...我分析没有内存泄漏!
    • motionBegan:withEvent: 何时调用?表重新加载后?移动数组的初始化以查看是否加载,并在将对象添加到数组后重新加载表
    • arrayOfFavourites = [[NSMutableArray alloc] init];如果内存泄漏,以上述方式分配 iVar 是一个明显的情况。
    • @ShantiKamichetty:仅当阵列未释放时。仅分配不是泄漏的证据。 (在 ARC 下,它被释放;明确的release 消息不是必需的或有效的。)
    【解决方案2】:

    你在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section中分配你的数组

    尝试将其分配到其他地方。

    【讨论】:

    • 我把分配和初始化放在了 initWithStyle: 方法中但是还是不行
    【解决方案3】:

    你可以在tableView:numberOfRowsInSectionMethod中分配arrayOfFavorites,但是你首先需要检查它是否为nil。

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

    你应该在 dealloc 方法中释放它:[arrayOfFavorites release]

    【讨论】:

    • 我把它放在 initWithStyle 方法中,我登录了 numberOfRowsInSection: if it is nil and it was!
    • 好吧更奇怪......我所做的是将计数记录在 numberOfRows.... 部分....在我的其他类中,当我将对象添加到 arrayOfFavourites 我也重新加载最喜欢的班级的表格视图。所以我摇了摇设备,所以它再次被记录下来,结果变成了一个计数......好!我还在视图加载时记录它,当视图加载时它被记录为 0... 奇怪!!!!!!我不知道为什么当我进入它的视图时它会为零……你知道为什么吗???
    • 另外,如果我尝试在不同的文章中摇两次(WEBVIEWCONTROLLER,这是一个网站,每个单元格的 webView 都会更改),那么数组仍然是一个然后为零!又奇怪了!那么............应用程序崩溃......
    【解决方案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;
     }
    

    在此代码中,listfav 仅存在于此方法的主体中,尝试获取它们已完成的值将失败,因为 listfav 在此之外不存在方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多