我目前正在尝试将图像显示在我的表格视图中。但是,看起来当它获得最后一张图像时,它会将所有图像设置为该图像。
图片来自我的第一篇博文。
这是我的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];
}
你们有没有人知道如何让这个工作?
答案 0 :(得分:1)
您正在使用ivar imageURL
设置表格中每行的图像。这当然意味着每一行都将具有相同的图像。您需要为行特定数据中的每一行存储单个图像URL。
您似乎在行特定数据中设置了url
键。在设置每个单元格时使用它代替imageURL
。
另请注意,在每个单元格的主线程上加载NSData
对象是一个非常糟糕的主意。如果Internet连接速度很慢,这将使您的UI非常不稳定且无响应。有许多解决方案可以在后台加载图像。
更新
试试这个:
NSURL *url = feeds[indexPath.row][@"url"];
NSData *imageData = [NSData dataWithContentsIfURL:url];