I have been trying to follow this SO thread but to no avail. The problem I get is that the image does not show which I assume is because the height is null when i set the heightForRowAtIndexPath to return image.size.height.
Feed.h
#import "AFNetworking.h"
#import <UIKit/UIKit.h>
@interface Feed : UITableViewController
@end
Feed.m
#import "Feed.h"
#import "NSDictionary+Feed.h"
#import "NSDictionary+Feed_package.h"
#import "UIImageView+AFNetworking.h"
#import "FeedItem.h"
@interface Feed ()
@property(strong) NSDictionary *json_from_url;
@property(strong) NSMutableDictionary *ImageHeightDic;
@end
static NSString * const BaseURLString = @"https://xxxx";
@implementation Feed
- (void)viewDidLoad {
[super viewDidLoad];
self.randomSelection = [[NSMutableDictionary alloc] init];
NSURL *url = [NSURL URLWithString:BaseURLString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
self.json_from_url = (NSDictionary *)responseObject;
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
[operation start];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSArray *feed_array = [self.json_from_url feed_array];
return [feed_array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FeedItem *feedcell = [tableView dequeueReusableCellWithIdentifier:@"FeedItemCell"];
if(!feedcell){
[tableView registerNib:[UINib nibWithNibName:@"FeedItem" bundle:nil] forCellReuseIdentifier:@"FeedItemCell"];
feedcell = [tableView dequeueReusableCellWithIdentifier:@"FeedItemCell"];
}
NSDictionary *daysWeather = nil;
NSArray *feed_array = [self.json_from_url feed_array];
daysWeather = feed_array[indexPath.row];
feedcell.captionLabel.text = [daysWeather feed_caption];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[daysWeather image_url]]
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:60];
UIImage *placeholderImage = [UIImage imageNamed:@"feed_image_loading"];
[feedcell.imageLabel setImageWithURLRequest:request
placeholderImage:placeholderImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
feedcell.imageLabel.image = image;
[self.ImageHeightDic setObject:image forKey:[NSString stringWithFormat:@"%li,%li",(long)indexPath.row,(long)indexPath.section]];
} failure:nil];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
[feedcell setNeedsLayout];
return feedcell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = (UIImage *)[self.ImageHeightDic objectForKey:
[NSString stringWithFormat:@"%li,%li",
(long)indexPath.row,(long)indexPath.section]];
return image.size.height;
// Here is where i think the problem is because it works
// when i comment out the above and uncomment below
//return 40;
}
@end