2017-07-25 32 views
0

我使用以下代碼從站點下載RSS提要。我想添加一個圖像到飼料有一個更現代的界面,因爲這個代碼只下載新聞標題。我不知道該怎麼做。從URL推送UITableViewCell中的圖像

小貼士?

@interface MasterTableViewController() { 

    NSXMLParser *parser; 
    NSMutableArray *feeds; 
    NSMutableDictionary *item; 
    NSMutableString *title; 
    NSMutableString *link; 
    NSString *element; 
} 

@end 

@implementation MasterTableViewController 

@synthesize recipe = _recipe; 
@synthesize tableView =_tableView; 


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

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    feeds = [[NSMutableArray alloc] init]; 
    NSURL *url = [NSURL URLWithString:[_recipe stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
    parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
    [parser setDelegate:self]; 
    [parser setShouldResolveExternalEntities:NO]; 
    [parser parse]; 
    [self.navigationController.navigationBar setTitleTextAttributes: 
    @{NSForegroundColorAttributeName:[UIColor whiteColor]}]; 
} 


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

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)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]; 
} 


#pragma mark - Navigation 


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

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return 60; 
} 

@end 

回答

1

您可以在cellForRowAtIndex中使用NSURLSession來獲取數據並在單元背景中顯示它。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 
cell.textLabel.text = [[feeds objectAtIndex:indexPath.row] objectForKey: @"title"]; 

NSError *error; 
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil]; 
NSURL *url = [NSURL URLWithString:[items objectAtIndex:indexPath.row]objectForKey:"link"]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                cachePolicy:NSURLRequestUseProtocolCachePolicy 
               timeoutInterval:60.0]; 

[request setHTTPMethod:@"POST"]; 

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    //data will return an imageData 
    UIImage *image = [UIImage imageWithData:data]; 
    cell.backgroundImage = image; 
}]; 

[postDataTask resume]; 
return cell; 
} 
+0

'沒有已知的類方法選擇「URLWithString:objectForKey'和'住宅 '和backgroundImage的UITableViewCell * ''' 不是種類型的目標發現' – Taprolano

+0

行更改爲: NSURL * URL = [NSURL URLWithString: [[items objectAtIndex:indexPath.row] objectForKey:「link」]]; ,你可以寫 cell.backgroundColor = [UIColor colorWithPatternImage:image]; – anamika41