2011-12-19 62 views
0

这个应用程序是一个带有标签栏控制器的表视图。我正在记录数组的数量:arrayOfFavourites,即使我添加一个对象仍然有一个零值,我的相关代码,显示的所有对象在代码中分配和初始化(先前或现在),一些是实例,一些是属性:addObject:要数组不工作(数组仍然没有)

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; 

} 

Favourites.m:

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

arrayOfFavourites = [[NSMutableArray alloc] init]; 


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

return [arrayOfFavourites count]; 
} 

回答

2

你为什么inializing在tableview:numberOfRowsInSection数组?这将导致阵列在每次重新加载表视图时被重置。这可能是你的问题。

+0

这也可能是另一个有关内存泄漏的问题。 – 2011-12-19 12:32:15

+0

我把分配和初始化在initWithStyle:方法,但仍然无法正常工作...我分析没有内存泄漏! – user973985 2011-12-19 12:39:51

+0

何时motionBegan:withEvent:被调用?表重新加载后?移动数组的初始化以查看是否加载,并在将对象添加到数组后重新加载表 – 2011-12-19 12:54:49

0

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

尝试到别处分配它。

+0

我把配置和初始化在initWithStyle:方法,但仍然不起作用 – user973985 2011-12-19 12:39:29

0

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

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

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

+0

仍然无法正常工作,奇怪.... – user973985 2011-12-19 12:55:25

+0

我把那在initWithStyle方法,我登录 numberOfRowsInSection: 如果它是零,它是! – user973985 2011-12-19 12:59:31

+0

好的,甚至更奇怪......我所做的是在numberOfRows ....部分中记录计数......在我的其他课程中,当我将该对象添加到arrayOfFavourites时,我还重新加载了最喜欢的类的表格视图。所以我摇晃了设备,所以它再次被记录下来,结果变成了一个数字....好!我也记录它的视图加载时,视图加载它被记录为0 ... WEIRD !!!!!我不知道为什么当我进入它的视图时它是零。你知道为什么吗? – user973985 2011-12-19 13:08:45

0
-(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没有该方法之外存在的价值。