這個應用程序是一個帶有標籤欄控制器的表視圖。我正在記錄數組的數量: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];
}
這也可能是另一個有關內存泄漏的問題。 – 2011-12-19 12:32:15
我把分配和初始化在initWithStyle:方法,但仍然無法正常工作...我分析沒有內存泄漏! – user973985 2011-12-19 12:39:51
何時motionBegan:withEvent:被調用?表重新加載後?移動數組的初始化以查看是否加載,並在將對象添加到數組後重新加載表 – 2011-12-19 12:54:49