我使用的XML文件是:
<note>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<URL>http://192.168.1.75/one.png</URL>
</note>
在这个XML中,我检索了文本,但是我无法显示XML文件中链接的图像。 我想在表格单元格中显示图像。我已经给出了我使用的编码如下。
在RootViewController.m中
@implementation RootViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [appDelegate.books count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
cell.text = aBook.heading;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// Set up the cell
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if(bdvController == nil)
bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
bdvController.aBook = aBook;
[self.navigationController pushViewController:bdvController animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to add the Edit button to the navigation bar.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = @"Books";
}
XMLAppDelegate.m:
@implementation XMLAppDelegate
@synthesize window;
@synthesize navigationController, books;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSURL *url = [[NSURL alloc] initWithString:@"http://192.168.1.75/second.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
Book.m:
@synthesize heading, body, URL;
XMLParser.m:
@implementation XMLParser
- (XMLParser *) initXMLParser {
[super init];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"note"]) {
appDelegate.books = [[NSMutableArray alloc] init];
}
if([elementName isEqualToString:@"note"]) {
}
NSLog(@"Processing Element: %@", elementName);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"note"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
if([elementName compare:@"heading"]==0) {
aBook.heading=currentElementValue;
}
if([elementName compare:@"body"]==0) {
aBook.body=currentElementValue;
}
if([elementName compare:@"URL"]==0) {
aBook.URL=currentElementValue;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
BOOk DetailViewController.m:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.section == 0)
{
cell.text=aBook.heading;
}
if(indexPath.section == 1)
{
cell.text=aBook.body;
}
if(indexPath.section == 2)
{
cell.image=aBook.URL;
}
return cell;
}
- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:@"Title"];
break;
case 1:
sectionName = [NSString stringWithString:@"Author"];
break;
case 2:
sectionName = [NSString stringWithString:@"Summary"];
break;
}
return sectionName;
}
答案 0 :(得分:0)
cell.image=aBook.URL;
网址不是图片制作。 (提示:根据URL的内容创建UIImage。)
答案 1 :(得分:0)
你必须从URL获取图像。您无法直接从网址显示图片。
NSData * data = [NSData dataWithContentsOfURL:url]; UIImage * image = [UIImage imageWithData:data];
此代码可以帮助您。