addObject:to array not working(array still nil) [英] addObject: to array not working (array still nil)

查看:202
本文介绍了addObject:to array not working(array still nil)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个应用程式是一个带有标签列控制器的表格检视。我记录数组的计数:arrayOfFavourites,即使我添加一个对象继续有一个nil值,我的相关代码,显示的所有对象分配和初始化在代码(上一个或现在)一些是实例和一些属性:

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:

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:

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:

Favourites.m:

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

arrayOfFavourites = [[NSMutableArray alloc] init];


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

return [arrayOfFavourites count];
}


推荐答案

tableview:numberOfRowsInSection ?这将导致每次重新加载表视图时重置数组。这可能是你的问题。

Why are you inializing the array in tableview:numberOfRowsInSection ? This will cause the array to be reset each time table view is reloaded. This could be your issue.

这篇关于addObject:to array not working(array still nil)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆