Simple: I want the size of the picture with width of the screen and the height calculated to keep the aspect ratio, similar to 9gag app.
How can I make dynamic height of cell in PFQueryTableViewController, in my cell I have one label and PFImageView in a Custom cell and loading works fine, but the pictures are not changing the height of the cell and all of the cells have the same height. I am stuck on this problem for third day and it looks like error from the parse.com framework. I was able to change the cell height when I was working with UITableViewController but the parse framework ignores it.
I am using storyboard and tried that with autolayout constraint and without it. TableViewController.swift
import UIKit
import Parse
import ParseUI
import Bolts
class TableViewController: PFQueryTableViewController {
// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
super.init(style: style, className: className)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Configure the PFQueryTableView
self.pullToRefreshEnabled = true
self.paginationEnabled = false
}
// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {
// Start the query object
var query = PFQuery(className: "Image")
query.whereKey("deleted", equalTo: 0)
query.orderByDescending("createdAt")
return query
}
//override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
if cell == nil {
cell = CustomTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "CustomCell")
}
// Extract values from the PFObject to display in the table cell
if let name = object?["caption"] as? String{
cell.postHeadlineLabel.text = name
}
// display initial image
var initialThumbnail = UIImage(named: "question")
cell.postImageView.image = initialThumbnail
// extract image
if let thumbnail = object?["image"] as? PFFile {
cell.postImageView.file = thumbnail
cell.postImageView.loadInBackground()
}
cell.postImageView.contentMode = UIViewContentMode.ScaleAspectFit
cell.postImageView.clipsToBounds = true
cell.actualWidth = cell.postImageView.image!.size.width
cell.actualHeight = cell.postImageView.image!.size.height
return cell
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
var cell = tableView.dequeueReusableCellWithIdentifier("CustomCell") as! CustomTableViewCell!
let aspect = cell.actualWidth / cell.actualHeight
var height: CGFloat = cell.postImageView!.frame.size.width * aspect
return height
}
}
CustomTableViewCell.swift
import UIKit
import Parse
import ParseUI
class CustomTableViewCell: PFTableViewCell {
var actualHeight: CGFloat = 10
var actualWidth: CGFloat = 10
@IBOutlet weak var postHeadlineLabel: UILabel!
@IBOutlet weak var postImageView: PFImageView!
}
I found some advices online but it doesnt work and seems like error of the parse framework with iOS8 I tried
cell.postImageView.contentMode = UIViewContentMode.ScaleAspectFit
cell.postImageView.clipsToBounds = true
cell.sendSubviewToBack(cell.postImageView)
or
override func awakeFromNib() {
self.setTranslatesAutoresizingMaskIntoConstraints(false)
}
Update - parse DB: https://www.anony.ws/image/D6tp