hellosheikh hellosheikh - 3 months ago 18
iOS Question

Self sizing TableView cell with image and label

hello I have a dynamic TableViewController in which there is one cell which has one image, and labels. I have set constraint of all of them. The problem is its not viewing correctly.

enter image description here

This is how its viewing

enter image description here

Code:

override func viewDidLoad() {
super.viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 103

}



override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

tableView.setNeedsLayout()
tableView.layoutIfNeeded()
self.tableView.reloadData()





}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

tableView.reloadData()
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

return 1


}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return newarray.count
}

override
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCellWithIdentifier("requestcell", forIndexPath: indexPath) as! NewRequestTableViewCell

let image : UIImage = UIImage(named: "placeholder_product_image")!
cell.imageView!.image = image
cell.requestTitle.text = "IPHONE 6s PLUS"
cell.DestinationCountry.text = "Pakistan"

return cell

}


I don't know what else to do. Please help how can I have row like I designed

Answer

The problem here is the height of your cell add the following methods in your code: (replacing of course <#cell_height#> by the value of your choice, or by UITableViewAutomaticDimension)

override func tableView(_ tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
    return <#cell_height#>;
}