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()
{
}
Related
I have configured a tableview in my main storyboard which has a button in the prototype cell. However, when I run it, the tableview cells show up without the button. Here is an image of my storyboard:
Can someone explain why it won't show up when I run the program?
Checklist:
1. Set tableView's delegate and dataSource to the controller, i.e.
tableView.delegate = self
tableView.dataSource = self
2. Check if the height of tableViewCell is not 0.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200.0 //Actual height of the cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100.0
}
3. What constraints have you added to the button? Try adding centeredHorizontally and centeredVertically constraints to the UIButton.
The problem is with the height of the table cell. Button is not showing because the height of the cell is small. You must You have to implement the tableView heightForRowAt indexPath Method.
Before that Assign Delegate and DtataSource Method to the tableView.
override func viewDidLoad(){
super.viewDidLoad()
tableview.delegate = self
tableview.datasource = self
}
If already assign then.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 105
}
Or I think you can also adjust the height from the Size Inspector check the custom height box and provide custom height. And also set proper constraint.
set background colour of button so that we can check button is created or not. or make sure you can add constraints of UIButton
Just place a break point on your UITableViewDataSource methods. If they are not calling then just go on your storyboard file and set the tableview delegate and datasource in your view controller.
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 static UITableView and one of my rows have a UITextView inside of it with autolayout pinned top to bottom left to right as 0.
I want the UITableView's contentView to resize depending on the content of the UITextView however I'm having difficulties working around this. I've scrapped the code I originally used as it wasn't working at all. Any solutions?
If the content of the textview is static.
Set SrollingEnabled to false in UITextView.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
If the content of the textview is dynamic.
Do the same thing as static and set the delegate for UITextView to self. Then in UITextview delegate.
func textViewDidChange(_ textView: UITextView) {
tableView.beginUpdates()
tableView.endUpdates()
}
This will definitely work.
I have a tableView, and within each UITableViewCell (call this parentCell), I have another tableView which has cells (call this childCell). I am using UITableViewAutomaticDimension to dynamically resize both of the cells height. I am successfully able to resize the height of the childCell this way, however, I am not able to apply UITableViewAutomaticDimension to the parentCell, since there when heightForRow is called, the childCell's height have yet to be estimated.
Is it possible to use UITableViewAutomaticDimension to estimate the parent cells height, which will be the height of all the childCell's height added together? Or am I going to have to do this manually?
//in ViewDidLoad
parentTableView.rowHeight = UITableViewAutomaticDimension
//in ParentCell's tableView's cell for row
cell.childTableView.rowHeight = UITableViewAutomaticDimension
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
The only way to do this was to use sections instead of placing a tableview within a cell.
I am new to IOS and anyhow i have implemented the FZAccordion from HERE. Now my problem is when i have a long text in header section and cells it goes beyond the bounds.So how can i get rid of this problem so that no matter how long is the text it always fits in the screen.
Thank you.
You can use
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView,estimatedHeightForRowAtindexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension}
This will automatically resizes your text in your cells.