Reputation: 183
The tableViews row heights are not sizing themselves correctly, I am trying to get the tableViews row heights to size according to the aspect ratio below is the code I have so far for cellForRowAt
and for heightForRowAt
.Also some images are being downloaded twice while others aren't downloaded at all.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reuse", for: indexPath) as! TableViewCell
let downloadURL = URL(string: self.imageURLS[indexPath.row])
if let image = imageCache.object(forKey: self.imageURLS[indexPath.row] as NSString)
{
cell.cellImageView.image = image
}
else{
URLSession.shared.dataTask(with: downloadURL!, completionHandler: {(data,response,error) in
if error != nil {
print(error!)
return
}
let downloadedImage = UIImage(data:data!)
let aspect = CGFloat((downloadedImage?.size.width)!/(downloadedImage?.size.height)!)
self.imageCache.setObject(downloadedImage!, forKey: self.imageURLS[indexPath.row] as NSString)
DispatchQueue.main.async {
cell.cellImageView.image = downloadedImage
cell.cellImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width/aspect)
cell.imageView?.contentMode = .scaleAspectFit
// cell.cellImageView.heightAnchor.constraint(equalTo: cell.cellImageView.widthAnchor, multiplier: aspect)
tableView.reloadRows(at: [indexPath], with: .top)
}
}).resume()
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
UPDATED CODE based on suggestions
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Reuse", for: indexPath) as! TableViewCell
let downloadURL = URL(string: self.imageURLS[indexPath.row])
if let image = imageCache.object(forKey: self.imageURLS[indexPath.row] as NSString)
{
cell.cellImageView.image = image
}
else{
URLSession.shared.dataTask(with: downloadURL!, completionHandler: {(data,response,error) in
if error != nil {
print(error!)
return
}
let downloadedImage = UIImage(data:data!)
let aspect = CGFloat((downloadedImage?.size.width)!/(downloadedImage?.size.height)!)
self.imageCache.setObject(downloadedImage!, forKey: self.imageURLS[indexPath.row] as NSString)
DispatchQueue.main.async {
cell.cellImageView.image = downloadedImage
cell.cellImageView.heightAnchor.constraint(equalTo: cell.cellImageView.widthAnchor, multiplier: aspect).isActive = true
cell.imageHeight.constant = (cell.bounds.width * (cell.cellImageView.image?.size.height)!)/(cell.cellImageView.image?.size.width)!
cell.layoutIfNeeded()
tableView.reloadRows(at: [indexPath], with: .top)
}
}).resume()
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
print("UITableViewAutomaticDimension: \(UITableViewAutomaticDimension)")
return UITableViewAutomaticDimension
}
}
Upvotes: 0
Views: 441
Reputation: 100503
1- Try to save the image and then reload the table
NSString *getImagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg",n1.Id]];
if([[NSFileManager defaultManager] fileExistsAtPath:getImagePath])
{
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
cell.leftImageV.image =img;
[cell.activity stopAnimating];
cell.activity.hidden=YES;
}
else
{
[cell.activity startAnimating];
cell.activity.hidden=NO;
cell.leftImageV.image = nil;
NSLog(@"xdasdxsadxa %@",n1.mainImgStr);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
__block NSData * imageData = nil;
dispatch_sync(concurrentQueue, ^{
imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:n1.mainImgStr]];
//Add the file name
[imageData writeToFile:getImagePath atomically:YES]; //Write the file
});
dispatch_sync(dispatch_get_main_queue(), ^{
if(imageData)
{
[self.tableView reloadData];
}
});
});
}
2- regarding aspect ratio , drag the height of the imageView as IBOutlet
and do this in cellForRowAt
cell.imageHeightCon.constant = (cellWidth*imageRealHeight)/imageRealWidth
note: here i assume that imageview width is equal to cell width ,it's better to give it tableViewWidth in proportional to viewController's view as cellWidth here is not yet rendered say tableView takes full screen width then set cellWidth to self.view.frame.size.width
3- don't forget to put this line before return cell
cell.layoutIfNeeded()
Upvotes: 1