在我的RSS feed应用程序中,我通过输入didStartElement方法使用NSXmlParser解析图像:
If ([element isEqualToString: @ "item"]) {
Item = [[NSMutableDictionary alloc] init];
Title = [[NSMutableString alloc] init];
Link = [[NSMutableString alloc] init];
PubDate = [[NSMutableString alloc] init];
ImageUrl = [[NSMutableString alloc] init];
}
If ([element isEqualToString: @ "enclosure"]) {
NSString * name = [attributeDict objectForKey: @ "url"];
NSLog (@ "The URL is:% @", name);
}
在didEndElement方法中:
If ([elementName isEqualToString: @ "item"]) {
[Item setObject: title forKey: @ "title"];
[Item setObject: link forKey: @ "link"];
[Item setObject: pubDate forKey: @ "pubDate"];
[Item setObject: imageUrl forKey: @ "url"];
[Feeds addObject: [item copy]];
}
确定。这是有效的,我拥有它们的链接。
现在我想将这些URL加载到我的UITableView中的单元格中。所以我使用了'SDWebImage'窗格。
在cellForRowAtIndexPath方法中,我有:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
cell.textLabel.numberOfLines = 0;
[cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping];
cell.detailTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
NSString *str = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
cell.detailTextLabel.text = str;
NSString * string = [[feeds objectAtIndex:indexPath.row] objectForKey: @"url"];
NSLog(@"THE URL IS: %@", string);
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"placeholder.gif"]];
return cell;
}
但没有发生。这是我目前的情况。
如何解决?
答案 0 :(得分:1)
sd_setImageWithURL :(可以为空的NSURL *)网址
API允许可以为空的值,因此您需要在使用之前检查string
和[NSURL URLWithString: string]
是否为非零。