I have a UITableView cell with a stack view inside. When the cell is tapped the data source changes and the table view is reloaded. The stack view will now have more views inside and the cell is bigger. However sometimes when I scroll the table there is jerky behaviour. It's almost like the cell size was calculated wrong or something (even though it looks fine). Once the tableview has jerked once it is fine and doesn't do it again until I tap the cell and it adds more stack views.
I am using UITableViewAutomaticDimension on the table view. I have tried removing the cell and the table doesn't jerk. It's defiantly the stack view causing issues.
I set my estimated row height to as close as possible to the calculated height tableView.estimatedRowHeight = 270. No affect. I have also tried implementing the delegate and it makes no difference. I have tried many combinations or sizes and the result is the same. Any idea on what I am doing wrong here? Do stack views in cells just suck?
I think you are on the right track about estimatedRowHeight causing trouble. I encounter this jerking problem and in pretty much every tableView with varying element size. What usually does the job is "caching" cell heights and returning them in delegate, something like:
class MyViewController: UIViewController {
fileprivate var cachedCellHeights = [IndexPath: CGFloat]()
//your code here
}
extension MyViewController: UITableViewDelegate {
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cachedCellHeights[indexPath] = cell.frame.height
}
public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
if let height = cachedCellHeights[indexPath] {
return height
}
return 270
}
}
It should work as long as you configure your cell (i.e. add new views to stack view) in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath).
The same applies to section headers.
Related
According to the following screenshot I have a table view cell with couple of stack views inside the cell. I want to hide the red circled stack view when it's inside label values are empty. To achieve this specific thing what I have done is just hiding the related stack view as the following screenshot. The related stack view is hiding. That's fine. But I want it to be hidden that empty area and adjust the height of the cell accordingly. I am not using the following function as well.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
}
I have used the following function as well. But it's still the same.
override func viewWillAppear(_ animated: Bool) {
SalaryDetailTableView.rowHeight = UITableViewAutomaticDimension
SalaryDetailTableView.estimatedRowHeight = 65.0
}
Could anyone please help me to sort out the issue ?
https://imgur.com/a/hMYA3qT
https://imgur.com/a/Dye8pBT
From your first screenshot I can see that you are using AutoLayout. Wrap all your stack views in a base stack view, pin it to the cell and check and fix your other constraints if needed. After that, you need to specify
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
If your constraints are right, AutoLayout will take care of collapsing or expanding the cell when you hide your inner stack view.
The problem here is I want the UITableViewCell to fully take the view of the cell and once I swipe down the other cell has to appear. What I have here is the other cell also appears on the same UITable screen. As you can see the title of the other news also can be seen in the UITableView. I want the first data to cover up the entire screen and once I swipe down the next data(title,image,description) has to be added.
In viewDidLoad(), make paging enabled:
myTableView.isPagingEnabled = true
then assign height to your cells,
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return self.myTableView.frame.size.height
}
I have a UITableView that holds cells that can show video, html, and images, and a UI of audio playback. That's effectively 3 different cells that could potentially be in the tableView in any given point in time. Cells of the same type can have different heights. That's the problem I'm trying to solve. Out of all the tutorials I've watched/read through, none taught me how to set the calculate then set the heights dynamically. I've only read stuff that show you how to tableViews with different cells that have static heights. Any suggestions on how to solve this problem?
Apply auto-layout in table view cell for cell-items and bound it with both top and bottom and use estimatedHeightForRowAt as:
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Or you can manually set the height for different rows as:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50 // rather than 50 return height you need to set
}
I have a problem with my UITableView sections (and their height, I guess).
Within a section header, I have a view (named cornerRadiusView) within which there are few labels. These labels have dynamic content (fetched from a REST service). So whenever a UILabel height is increased, I am increasing the height of the cornerRadiusView to properly accommodate the content.
The problem is, all this works as expected only when I scroll my tableView. Till then, the heights are improper.
Here I am posting GIF to show what's going on.
I can give more details and code as needed. Could someone please look at where there is a problem?
You can use below functions
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
And reload table view in below method
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableview.reloadData()
}
And if you are maintaining dynamic height then manage those height in cell
override func layoutSubviews()
{
}
I have a table view controller with a navigation controller, and when there is no data for the table view, I want to display a single table cell with instructions on how to add new data. I can display the table cell, but I do not understand how I can get the height of the cell to span the visible height (table height - navigation item height). My goal is to have the label inside of my table cell be vertically centered in the device. I've tried using self.tableView.frame.height, but that gives me the scrollable height, which is larger than the visible area.
Edit: I failed to mention I'm doing this from tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat.
I have found a Good Enoughâ„¢ answer to my own question:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
tableView.isScrollEnabled = false
return tableView.frame.height - (self.navigationController?.navigationBar.frame.height)!
}