Tableview set row height dynamically based on content in swift? - swift

I am struck in setting tableview cell row height in swift , On scrolling the row height is changing, please check the screen shot.
For the first time its scrolling perfect on scrolling multiple times the height is not proper. I am using too many custom tableview cells
In viewDidLoad
I added this
self.tableView.rowHeight = UITableViewAutomaticDimension
and also added these delegate methods.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
Can anyone guide me to achieve this in proper way.

Related

tableview scrolls when adding/deleting data in swift 2.3

Re-wording this question as I understand the problem better:
I have a UITableview and I am reloading data from Firebase when theres any activity. I use
tableView.estimatedRowHeight = 330
tableView.rowHeight = UITableViewAutomaticDimension
So anytime my tableview source gets updated - a new post gets added or deleted, my UI tableview will scroll one cell up or down to accomodate for this new activity. How do I make sure my cell stays in the same position when tableview gets updated ?
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 1000
}

Dynamic cell height with async images in Swift 2

I'm trying to build a Facebook-like feed. I'm using a standard Tableview with autolayout constraints set up in storyboard.
The width should be 100% of the viewport and the height according to the aspect ratio of the image.
I'm using Haneke's hnk_setImageFromURL for async image loading from the server.
I saw here the option to use tableview.beginUpdates() or endUpdates() and it causes the first render to work alright, but once I start scrolling the height calculation goes wrong, cells appear blank and/or the app crashes due to indexOutOfBounds.
Is manually calculating the height in heightForRowAtIndexPath with pre-given aspect ratio from the server the only option?
My relevant part of the code
ViewController:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 500
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("FeedItem", forIndexPath: indexPath) as! FeedItemTableViewCell
content.hnk_setImageFromURL(NSURL(string: contentImage)!, placeholder: nil, format: nil, failure: nil, success: {image in
cell.content.image = image
self.beginUpdates()
self.endUpdates()
}
)
}
P.S. A side effect caused by using Haneke is that I have to provide some initial size to the UIImageView, currently I'm using a placeholder image which causes irritating animations when the image is loaded from the server
Here instead of loading cell data in the cellForRowAtIndex method,I have created a subclass of TableViewCell. And I have used SDWebImage instead of Haneke.
Hope this helps

How can I change row height from cellForRowAtIndexPath?

I need to change row height in cellForRowAtIndexPath, using UITableView.
heightForRowAtIndexPath executes before cellForRowAtIndexPath, but in cellForRowAtIndexPath I'm settings content to rows, depended on which height must be recalculated.
You can't, the only place you can and should change is heightForRowAtIndexPath
But there are other solutions for you problem, that are explained very well here
I encourage you to check third solution whitch enable Row Height Estimation.
You can set the height in the viewDidLoad for all cells
self.tableView.rowHeight = 88.0
or use the event heightForRowAt where u have the IndexPath for the cell
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 88.0
}

How to make the height of tableviewcell self-adaption to the text-label in it?

I am creating a TableViewController, now there's some cells in it, the longth of texts in these cells are not the same, so I wanna make the height of the cells self-adaption to the text-label.
I was thinking to creat the label first, then set the height of cell to fix it, but it seems not work, here is the code.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let cell = tableView.cellForRowAtIndexPath(indexPath) as! FuckGFWQATableViewCell
return cell.question.bounds.height
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("fuckGFWQATableViewCellId", forIndexPath: indexPath) as! FuckGFWQATableViewCell
cell.question.numberOfLines = 0
cell.question.text = fuckGFWQuestions[indexPath.section]
return cell
}
But when the heightForRowAtIndexPath called, the label was not exist. What should I do?
With iOS8 you can use self sizing cells in UITableView.Checkout here. You do not need to calculate your UILabel's height and set your cell. You just need to :
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

Fill UITableViewCell with UITextView (Static Cells)

If I have a static uitableviewcell with a textview inside it, how am I able to set the height of the uitableviewcell to fit all of the textview content on it?
You can use this method.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
You can use an If-else condition to make it what ever height you want as long as you return a number(CGFloat)