我使用以下代码从网站下载RSS源。我想在Feed中添加一个图像以获得更“现代”的界面,因为此代码仅下载新闻标题。我不知道该怎么做。
提示?
XXXFactory
答案 0 :(得分: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;
}