Dynamic Cell Height with Sdwebimage

Dynamic cell height with SDWebImage

You just need to tell the whole tableView to refresh its view. Use this code in the view controller holding the tableView:

func refreshTableView() {
self.tableView.beginUpdates()
self.tableView.setNeedsDisplay()
self.tableView.endUpdates()
}

Using a delegate pattern tell the viewController to refresh the tableView, so something like:

func setCustomImage(image: UIImage) {
let aspect = image.size.width / image.size.height
let constraint = NSLayoutConstraint(item: postChart, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: postChart, attribute: NSLayoutAttribute.height, multiplier: aspect, constant: 0.0)
constraint.priority = UILayoutPriority(rawValue: 999)
aspectConstraint = constraint
postChart.image = image

// call this to refresh table
delegate.refreshTableView()
}

Post class:

class Post {
var title : String?
var source : String?
var chartURL : String?
var postChart: UIImage?
var category : String?
var rank : CGFloat?
var imageSize: CGSize?

init(data: NSDictionary) {
title = data["title"] as? String
source = data["source"]as? String
chartURL = data["chartURL"] as? String
postChart = data["postChart"] as? UIImage
category = data["category"] as? String
rank = data["rank"] as? CGFloat
}
}

Dynamic image height in tableview with using sdwebimage

Try this Helper method which created for my project.

-(void)imageWithURL:(NSURL *)imageurl WithIndexPath:(NSIndexPath *)indexpath WithCallback:(void(^)(UIImage *downloadedImage,BOOL isCache , NSIndexPath *imageIndexPath))callback{
if (![[SDWebImageManager sharedManager ]diskImageExistsForURL:imageurl]) {
[[SDWebImageManager sharedManager]downloadImageWithURL:imageurl options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
// resize image here
callback(image , NO,indexpath);
}];

}
else{
UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:[imageurl absoluteString]] ;
// resize image here
callback(image, YES ,indexpath);
}

}

Call this method in cellForRowAtIndexPath

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self imageWithURL:[NSURL URLWithString:objPreparations.strImageURL] WithIndexPath:indexPath WithCallback:^(UIImage *downloadedImage, BOOL isCache, NSIndexPath *imageIndexPath) {

if (downloadedImage) {
dispatch_async( dispatch_get_main_queue(), ^{
UITableViewCell *existcell = [tableView cellForRowAtIndexPath:imageIndexPath];
if (existcell) {
// assign image to cell here
[tableView reloadRowsAtIndexPaths:@[imageIndexPath] withRowAnimation:UITableViewRowAnimationNone];
}
});
}
}];
});

Dont Forgot

In viewDidLoad

tblView.estimatedRowHeight = 44.0 ;
tblView.rowHeight = UITableViewAutomaticDimension;

And also Give top,bottom,leading,trailing constraints to imageview and greater than equal height constraint to imageview

Dynamic image height in tableview not working for initial few cell

From your code work I found two things ,

  1. Never return UITableViewAutomaticDimension from estimatedHeightForRowAt because estimated height is never be Automatic otherwise x-code does not understand what height it should need to return. Sometimes it works but not considered as a good practice.

  2. You fetching the Images in the cellForRowAt method that means upto you fetch the image the height of cell is already set. So your cell images that height only.UITableViewAutomaticDimension works when system knows the height of that cell at the heightForRowAt method not after that.

Suggestion for your problem.

Once you fetch the data and then reload your tableView so that cell height is adjusted according to the image height. You can do this in paging also.



Related Topics



Leave a reply



Submit