此应用是带有标签栏控制器的表格视图。我正在记录数组的计数: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;
}
Favourites.m:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
arrayOfFavourites = [[NSMutableArray alloc] init];
NSLog(@"ROWS NO.");
NSLog(@"%i", [arrayOfFavourites count]);
return [arrayOfFavourites count];
}
答案 0 :(得分:2)
为什么要在tableview:numberOfRowsInSection
中初始化数组?这将导致每次重新加载表视图时重置阵列。这可能是你的问题。
答案 1 :(得分:0)
您要在-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
尝试将其分配到其他地方。
答案 2 :(得分:0)
您可以在arrayOfFavorites
中分配tableView:numberOfRowsInSectionMethod
,但首先需要检查它是否为零。
if( !arrayOfFavorites )
arrayOfFavoriges = [[NSMutableArray alloc] init];
您应该在dealloc方法中释放它:[arrayOfFavorites release]
。
答案 3 :(得分: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;
}
在此代码中list
和fav
只存在于此方法的正文中,尝试获取他们执行的值将失败,因为list
和{{1在该方法之外不存在。