2014-02-24 34 views
0

我有一個RSS提要正確顯示在我的UITableView中,但是我試圖添加來自RSS提要的圖像。以下是我正在使用的代碼。我已經刪除了我迄今爲止合併圖像的嘗試,只剩下標題和鏈接。任何幫助非常感謝。我不確定如何添加來自RSS提要的圖像到我的UITableView

#import "MasterViewController.h" 

#import "DetailViewController.h" 

@interface MasterViewController() { 
     NSXMLParser *parser; 
     NSMutableArray *feeds; 
     NSMutableDictionary *item; 
     NSMutableString *title; 
     NSMutableString *link; 
     NSString *element; 
} 

@end 

@implementation MasterViewController 


- (void)awakeFromNib 
{ 
     [super awakeFromNib]; 
} 

- (void)viewDidLoad { 
     [super viewDidLoad]; 
     feeds = [[NSMutableArray alloc] init]; 
     NSURL *url = [NSURL URLWithString:@"http://art-britain.co.uk/category/galleries/feed/"]; 
     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 

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

} 

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

      [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

你沒說明是哪裏的問題.. –

+0

對不起,我相當對此,我嘗試了幾種不同的方法將rss feed中的圖像合併到表格視圖中,但它們都未成功。所以我想知道是否有人可以建議如何將圖像合併到上面的代碼中? – user3344127

+0

一個非常好的方法是Apple提供的LazyTableImages https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html,並以您想要的方式使用「RSS」。 –

回答

1

RSSXML將包含一個圖像對象聲明如下:

<image> 
    <url>http://www.w3schools.com/images/logo.gif</url> 
    <title>W3Schools.com</title> 
    <link>http://www.w3schools.com</link> 
</image> 

你需要提取什麼是圖像的URL

然後,在圖像的cellForRowAtIndexPath開始下載:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    ... your code 

    __weak UITableViewCell *weakCell = cell; 

    dispatch_async(kBgQueue,^
    { 
     NSData *imgData = [NSData dataWithContentsOfURL:YOUR_EXTRACTED_URL]; 

     if (imgData) 
     { 
      UIImage *image = [UIImage imageWithData:imgData]; 

      if (image) 
      { 
       dispatch_async(dispatch_get_main_queue(),^
       { 
        cell.imageView.image = image; 
       }); 
      } 
     } 
    }); 

    return cell; 
} 

,並定義kBgQueue:

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 
+0

非常感謝,我已經使用了您的建議代碼,此刻我得到了'kBgQueue'的一個未聲明的標識符錯誤,我不知道該如何解決? – user3344127

+0

對不起老兄,我忘了發一行。我更新了我的答案。檢查它是否現在可以運行:) – RaffAl

+0

沒問題,再次感謝。所以這就是我的NSData行:'NSData * imgData = [NSData dataWithContentsOfURL:@「http://art-britain.co.uk/category/galleries/feed/」]'這是給我一個'不兼容指針'警告,並且應用程序一旦崩潰就崩潰。 – user3344127