我目前正在尝试让图像显示在我的表格 View 中。但是,看起来当它获取最后一个图像时,它会将所有图像设置为该图像。
该图片来 self 的第一篇博客文章。
这是我的 RSS 检测代码:
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return feeds.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"];
NSString *textFiltered = [[feeds objectAtIndex:indexPath.row] objectForKey: @"description"];
cell.detailTextLabel.text = [textFiltered stringByConvertingHTMLToPlainText];
//cell.detailTextLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"pubDate"];
//NSString *imageURLString = [[feeds objectAtIndex:indexPath.row] objectForKey: @"enclosure"];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];
return cell;
}
#pragma mark - parsing of RssFeed Values
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
element = elementName;
if ([element isEqualToString:@"item"]) {
item = [[NSMutableDictionary alloc]init];
title = [[NSMutableString alloc]init];
link = [[NSMutableString alloc] init];
desc = [[NSMutableString alloc] init];
image = [[NSMutableString alloc] init];
pubDate = [[NSMutableString alloc] init];
feedEnclosure = [[NSMutableString alloc] init];
encodedContent = [[NSMutableString alloc] init];
//imageURL = [[NSURL alloc] init];
}if ([element isEqualToString:@"enclosure"]){
NSString *imageStr = [NSString stringWithString:[attributeDict objectForKey:@"url"]];
imageURL = [NSURL URLWithString:imageStr];
}
}
-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if ([elementName isEqualToString:@"item"]) {
[item setObject:title forKey:@"title"];
[item setObject:link forKey:@"link"];
[item setObject:desc forKey:@"description"];
[item setObject:pubDate forKey:@"pubDate"];
[item setObject:feedEnclosure forKey:@"enclosure"];
[item setObject:encodedContent forKey:@"content:encoded"];
[item setObject:imageURL forKey:@"url"];
[feeds addObject:[item copy]];
}
}
-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
if ([element isEqualToString:@"title"]) {
[title appendString:string];
}else if ([element isEqualToString:@"link"]){
[link appendString:string];
}else if ([element isEqualToString:@"description"]){
[desc appendString:string];
}else if ([element isEqualToString:@"pubDate"]){
[pubDate appendString:string];
}else if ([element isEqualToString:@"content:encoded"]){
[encodedContent appendString:string];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
你们知道我如何让它发挥作用吗?
最佳答案
您正在使用 ivar imageURL
设置表中每一行的图像。这当然意味着每一行都会有相同的图像。您需要在特定于行的数据中存储每一行的各个图像 URL。
您似乎在特定于行的数据中设置了 url
键。在设置每个单元格时使用它而不是 imageURL
。
另请注意,在每个单元格的主线程上加载 NSData
对象是一个非常糟糕的主意。如果 Internet 连接速度很慢,这将使您的 UI 非常不稳定且 react 迟钝。在后台加载图片有很多解决方案。
更新:
试试这个:
NSURL *url = feeds[indexPath.row][@"url"];
NSData *imageData = [NSData dataWithContentsIfURL:url];
关于iOS - 检测到附件 URL,但未针对不同项目显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20319344/