How to set tableViewCell's Rows Height - swift

I would like to programmatically change my tableViewCell's Height,
How can i do so?
My TableView Example:-

In your viewcontroller set the row height via:
tableView.rowHeight = 88.8
You can also set the row height of an individual row by implementing:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 88.0
}
return 44.0
}

Related

Changing the height of cell depending on text height

I want to show a tableView with dynamic cell height. I found a way to change the height of my prototype cell in a tableView manually using this code. In this case the height is 400.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat (400)
}
In my cell the first part with the username (green and red) and the last part with the likes (yellow) has a fixed height with for example 60.
The height of the part in the middle (blue) should change depending on the text. So how can I do that?
I tried to get the label height with this.
override func awakeFromNib() {
super.awakeFromNib()
userComment.sizeToFit()
print(userComment.bounds.size.height)
}
But this always shows me 18. My aim is to use the first code above and return CGFloat ( 60 + 60 + dynamic label/userComment height)
This is how my tableView looks like.
extension ViewComments: UITableViewDataSource {
func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return table.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
let video: importComment
video = table[indexPath.row]
cell.userName.text = video.userName
cell.userGroup.text = poiNavigationName.title
cell.userComment.text = video.userComment
cell.userTime.text = "\(video.userTime!)"
cell.userLikes.text = "\(video.userLikes!)"
cell.userName.text = video.userName
cell.commentId.text = video.commentId
cell.kommentarCount.text = "\(video.kommentarCount!)"
cell.buttonAction = { [unowned self] in
let selectedIndexPath = table[indexPath.row].commentId!
ViewComments.commentIDNew = selectedIndexPath
}
return cell
}
/*
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat (400)
}*/
}
Updated Picture after removing heightForRowAt and awakeFromNib
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return CGFloat (400)
}
instead of using a hard coded value you can use a dynamic height.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
UITableView.automaticDimension
}
https://developer.apple.com/documentation/uikit/uitableview/1614961-automaticdimension
It seems to me you're describing something like this:
That's done entirely with the internal autolayout constraints of the prototype cell. You should not attempt to do this manually by returning a specific height for each cell; just let the runtime do it for you. It knows how to do this a lot better.
I solved it not following this tutorial and it works great. Tutorial

UICollectionView inside a UITableViewCell — dynamic height calculate isn't correct

I have a dynamic height collection view in table view cell. I used the approach #Igor in this topic.
UICollectionView inside a UITableViewCell -- dynamic height?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DraftCustomerTableViewCell", for: indexPath) as! DraftCustomerTableViewCell
cell.data = notificationDraftListViewData[indexPath.section]
cell.frame = tableView.bounds
cell.collectionView.reloadData()
cell.collectionViewHeightConstraint.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height
cell.layoutIfNeeded()
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
dict[indexPath] = cell.frame.size.height
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return dict[indexPath] ?? CGFloat(450)
}
The issue is when I expand the tableview section first time, the cell's height is not calculate right. After expanding first time, the collectionView's height can been calculated correctly. I try to let my data[0].isExpand is true without taping the button and find the table view's cell height is correct. Please see the demo. Is there some approach can avoid this issue? thanks.
Demo: https://youtu.be/YJCJChCU0wQ

How to make Custom Xib Cell Resize Correctly

I'm trying to make a custom xib cell resize based on really how much information the user enters.
I have tried setting the constraints of the xib cell, like this Constraints
I only have top and bottom constraints on the Label and the Date.
In the viewDidLoad() of the viewcontroller containing the cell and tableview i have
self.tableView.rowHeight = UITableView.automaticDimension
and I also have set
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 250
}
GIF of Resize Not Working

How to create dynamic height for TableView cell textview using Swift?

I am creating single Tableview custom cell row with top and bottom two textview. Here, bottom textview need to adjust height dynamically based on textview text length.
My Storyboard
enter image description here
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = 280.0
}
//Choose your custom row height
private func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 280.0;
}
// number of rows in table view
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.animals.count
}
// create a cell for each table view row
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
cell.sourceTextView.text = “ABCD….”
return cell
}
Just do this
func adjustUITextViewHeight(arg : UITextView)
{
arg.translatesAutoresizingMaskIntoConstraints = true
arg.sizeToFit()
arg.scrollEnabled = false
}
In Swift 4 the syntax of arg.scrollEnabled = false has changed to
arg.isScrollEnabled = false.

Dynamic cell size with resize cell animation

I have a table view with a dynamic height of cells. Above the table is menu with buttons. When I click on the menu button, the data is loaded into the table. When data is loaded, I want to have an animation in a cell that changes the height of a cell. I wonder how this can be done?
Thanks for the help.
Swift 4
Create a boolean variable in your model for checking if your cell is expanded or not.
if you want to expand the cell as long as your label height you should connect you labels constraint to your contentView in all directions and set number of lines to 0 in your storyboard.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
details[indexPath.row].cellIsOpen = !details[indexPath.row].cellIsOpen
detailTableView.reloadData()
detailTableView.beginUpdates()
detailTableView.endUpdates()
detailViewHeightConstraint.constant = CGFloat(detailTableView.contentSize.height) // the height of whole tableView
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if details[indexPath.row].cellIsOpen {
return UITableViewAutomaticDimension // or any number you wish
} else {
return 60 // default closed cell height
}
}
}
Also you can place this two lines in your viewDidLoad() function:
detailTableView.estimatedRowHeight = 60
detailTableView.rowHeight = UITableViewAutomaticDimension