【问题标题】:Problems with tableView and deallocated instancetableView 和释放实例的问题
【发布时间】:2013-11-07 00:16:49
【问题描述】:

使用 XCode (4.6) 提供的主视图模板创建一个简单的 RSS 提要阅读器。

应用程序加载第一个充满标题的屏幕,然后向下或向上滚动时,它会出错并出现错误。

我在控制台中收到的错误是:

[__NSArrayM retain]: message sent to deallocated instance 0x7522d40

跟踪将我带到我的 cellForRowAtIndex 函数中的这一行。 XCode 将此行标记为信号 SIGKILL。 (这可能是因为我按照调试教程中的说明设置了僵尸对象)。

NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];

这是该函数的其余部分。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString * cellId = @"feeds";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellId];

if(cell == nil){
    cell = [[UITableViewCell alloc] init];
    NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"title"]];
}

return cell;
}

这是我的 viewDidLoad 函数。

- (void)viewDidLoad
{
[super viewDidLoad];
self.nf = [[NewsFeed alloc]init];
[self.nf setFeedURL:[NSURL URLWithString:@"http://rss.cnn.com/rss/cnn_topstories.rss"]];
[self.nf retrieveFromInternet];

//For debugging. 
for(id newsItem in self.nf.newsStories){
    NSDictionary * d = (NSDictionary *)newsItem;
    NSLog(@"Title: %@\nDescription: %@", [d objectForKey:@"title"], [d objectForKey:@"description"]);
}

}

任何帮助将不胜感激。多谢你们。

【问题讨论】:

  • 尝试在创建单元格时将 init 替换为 initWithStyle:reuseIdentifier: -- 这是实例化单元格的常用方法。
  • 将可变字典更改为仅字典并检查??

标签: ios objective-c memory-management tableview


【解决方案1】:

由于某种原因我不能添加 cmets,但你应该移动

NSMutableDictionary * news = (NSMutableDictionary *)[self.nf.newsStories objectAtIndex:indexPath.row];
    [cell.textLabel setText:[news objectForKey:@"title"]];

超出了条件......我不确定你为什么要让它变得可变。另外,将单元格起始更改为:

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellId];

除此之外,如果您使用 ARC,请确保您的新闻源中的数组的属性设置为 strong。

【讨论】:

  • 嗯。仍然抛出同样的错误。我想知道问题是否出在我的 NewsFeed 实施中。谢谢你给我建议。
猜你喜欢
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多