2010-02-22 46 views
0

您好我有問題從XML文件加載圖像。我在想,如果如果你想顯示在您的表視圖圖像,那麼你必須要在您的表視圖一個UIImageView用於顯示圖像有人可能能夠幫助從XML的UIImage加載圖像

// 
// BookDetailViewController.m 
// XML 
// 
// Created by iPhone SDK Articles on 11/23/08. 
// Copyright 2008 www.iPhoneSDKArticles.com. 
// 

#import "BookDetailViewController.h" 
#import "Book.h" 

@implementation BookDetailViewController 

@synthesize aBook; 

/* 
// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
     // Custom initialization 
    } 
    return self; 
} 
*/ 

/* 
// Implement loadView to create a view hierarchy programmatically. 
- (void)loadView { 
} 
*/ 


// Implement viewDidLoad to do additional setup after loading the view. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.title = @"Book Detail"; 
} 

- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    [tableView reloadData]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
    // Release anything that's not essential, such as cached data 
} 

- (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]; 
    } 

    switch(indexPath.section) 
    { 
     case 0: 
      cell.text = aBook.title; 
      break; 
     case 1: 
      cell.text = aBook.author; 
      break; 
     case 2: 
      cell.text = aBook.summary; 
      break; 
     case 3: 
      UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com.au/logos/olympics10-icedance-hp.png"]]]; 

      break; 

    } 

    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:@"Book Summary"]; 
      break; 
    } 

    return sectionName; 
} 

- (void)dealloc { 

    [aBook release]; 
    [tableView release]; 
    [super dealloc]; 
} 


@end 
+0

這個代碼究竟有什麼問題? – Vladimir 2010-02-22 09:39:48

+0

另外,不要使用'dataWithContentsOfURL'。原因如下:http://developer.apple.com/library/ios/#qa/qa2010/qa1693.html – 2011-01-21 02:10:09

回答