我想显示网址中的图片
我在Objective c项目中使用表视图控制器。我能够完美地显示文本和链接,但无法显示Feed网址中的图像
我正在使用以下代码
**TableViewController.h**
#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController <NSXMLParserDelegate>
@property (strong, nonatomic) IBOutlet UITableView *TableView;
@end
TableViewController.m
#import "TableViewController.h"
#import "ViewController.h"
@interface TableViewController (){
NSXMLParser *parser;
NSMutableArray *feeds;
NSMutableDictionary *item;
NSMutableString *title;
NSMutableString *link;
NSString *element;
NSString *imageType;
NSString *imageUrl;
}
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
feeds=[[NSMutableArray alloc ] init];
NSURL *url = [NSURL URLWithString:@"https://www.nasa.gov/rss/dyn/breaking_news.rss"];
parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (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"];
// cell.imageView.image= [[feeds objectAtIndex:indexPath.row] objectForKey:@"title"];
return cell;
}
-(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];
}
if ([element isEqualToString:@"enclosure"]) {
imageType = [attributeDict objectForKey:@"type"];
imageUrl = [attributeDict objectForKey:@"url"];
}
}
-(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:imageType forKey:@"imageType"];
[item setObject:imageUrl forKey:@"imageUrl"];
[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];
}
}
-(void)parserDidEndDocument:(NSXMLParser *)parser {
[self.tableView reloadData];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *string = [feeds[indexPath.row] objectForKey:@"link"];
[[segue destinationViewController] setUrl:string];
}
}
@end
请帮我显示我坚持使用的图片。谢谢
答案 0 :(得分:1)
使用这种方式:
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];
UIImageview imgView = [[UIImageView alloc] initWithFrame:CGRectMake(150, 0, 48, 48)];
imgView.tag = 100;
//ssame way create a label
UILabel titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
titlelabel.tag = 102
[cell.contentview addsubview:imgview];
[cell.contentview addsubview:titlelabel];
}
UIImageview mImgView = (uiimageview *)[cell viewwithtag:100];
UILabel mLabel = (uilabel*)cell viewwithtag:102];
//set data
mlabel = //your label
mimgviw.imge = imageurl
答案 1 :(得分:0)
只需将此行添加到UITableViewCell
NSString *imgUrl = [[feeds objectAtIndex:indexPath.row] objectForKey:@"imageUrl"];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgUrl]]];
cell.imageView.image = image;