数组为零(即使在 addObject 时也是如此:)

发布于 2024-12-21 22:12:06 字数 2841 浏览 1 评论 0原文

可能的重复:
addObject:数组不起作用(数组仍然为零) < /p>

一切更新

此应用程序是带有选项卡栏控制器的表格视图。我正在记录数组的计数: 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];
}

收藏夹:

一类,在选项卡栏控制器上

WEBVIEWCONROLLER:

不同 Web 视图的控制器

LISTVIEWCONTROLLER:

数据提供者

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

Possible Duplicate:
addObject: to array not working (array still nil)

EVERYTHING UPDATED

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

Favourites.m:

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

    return [arrayOfFavourites count];
}

Favourites:

A CLASS, ON TAB BAR CONTROLLER

WEBVIEWCONROLLER:

CONTROLLER FOR DIFFERENT WEB VIEWS

LISTVIEWCONTROLLER:

DATA PROVIDER

Actually what happens is when i shake i reload the table view data and i add an object to an array (array of favorites) the count is one.... GOOD! But then when i shake again (in a different article, my app has different webViews when i press different cells.) It is still 1... weird.... if i go to favorites class that array remains one.. ok... so as you can see i am returning the arrayOfFavourites count to numberOfRowsInSection (which is 1) but no cells appear and the cellForRowAtIndexPath is never called (using an NSLOG) why is this happening I AM VERY ANNOYED!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乱世争霸 2024-12-28 22:12:06

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

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

因为您在每次调用 numberOfRowsInSection 时都重新初始化(并且可能泄漏)(每次调用都会调用该函数)表需要知道必须显示多少行的时间——即经常)。

In your Favourites.m numberOfRowsInSection function, looks like you should do this:

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

Because you're reinitializing (and likely leaking) in every single call to numberOfRowsInSection (which gets called each time the table needs to know how many rows it must display -- i.e. very often).

半窗疏影 2024-12-28 22:12:06

每次执行 -motionBegan:withEvent: 时都会创建和销毁一个新的收藏夹对象,并且每次执行 -tableView:numberOfRowsInSection: 时都会创建一个新数组。如果您希望数据在这些事件之外持续存在,则需要保留对象。

You create and destroy a new Favourites object every time you go through -motionBegan:withEvent:, and you create a new array every time you go through -tableView:numberOfRowsInSection:. If you want data to persist beyond those events, you need to keep the objects around.

赠佳期 2024-12-28 22:12:06

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

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

除非我错误地阅读了您的代码,否则我感觉您正在尝试对“易失性”对象进行持久化。

您需要为这些对象创建一个@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];
}

Let review this part of your code :

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

Unless I'm reading your code incorrectly I have the feeling that you are trying to have some persistence with "volatile" object.

You need to make a @property for those object to survive longer that one method call.
Every time you create a new object it's new and have no knowledge of the precedent one.

And if we look at this code :

-(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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文