2011-12-19 68 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沒有該方法之外存在的價值。