3

使用 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"]);
}

}

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

4

1 回答 1

2

出于某种原因,我无法添加评论,但你应该移动

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

超出条件......我不确定你为什么要让它变得可变。此外,将单元启动更改为:

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

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

于 2013-11-07T00:35:10.323 回答