addObject:数组不起作用(数组仍然为零)

发布于 2024-12-22 00:50:42 字数 2163 浏览 3 评论 0原文

这个应用程序是一个带有标签栏控制器的表格视图。我正在记录数组的计数: 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];
}

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:

  -(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:

You shake to favorite a cell

 -(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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

晚雾 2024-12-29 00:50:43

为什么要在 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.

め七分饶幸 2024-12-29 00:50:43

您正在 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 中分配数组,

尝试将其分配到其他地方。

you are allocating your array in -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

try to allocate it somewhere else.

日记撕了你也走了 2024-12-29 00:50:43

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

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

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

You could allocate the arrayOfFavorites in the tableView:numberOfRowsInSectionMethod, but then you first need to check if it is nil.

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

You should release it then in the dealloc method: [arrayOfFavorites release].

三人与歌 2024-12-29 00:50:43
 -(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仅存在于该方法的主体中,尝试获取它们所保存的值将失败,因为list 和 fav 在该方法之外不存在。

 -(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;
 }

In this code list and fav exist only in the body of this method, attempt to get to the value they have hold done to will failed, because list and fav doesn't exist outside that method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文