Fill UITableViewCell with UITextView (Static Cells) - swift

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)

Related

How to stretch cell to full screen?

I have a UIView inside a table cell. I need to stretch my cell on full screen.
In my table view I have this:
table.rowHeight = UITableView.automaticDimension
But now I can't to make this.
My code is:
containerView.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
verticalStackView.snp.makeConstraints { make in
make.center.equalToSuperview()
make.leading.trailing.equalToSuperview().inset(20)
}
I need to get this:
But I have:
So UITableView.automaticDimension will tell the program to find the minimum height this cell can be without any clipping (to simplify it)
What you'll need to do is intercept the cell in
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if self.myData.isEmpty {
return tableView.frame.height
}
return UITableView.automaticDimension
}
Assuming you show that cell when there Is no data to be shown, you can just do a check to see if you data array isEmpty, and if so, make the cell the height of the tableView

Tableview set row height dynamically based on content in 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.

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