How to increase table view height inside table view cell - ios

i have table view inside table view cell, need to set table view height depending on table view content, i tried below code
extension tableViewCell: UITableViewDelegate{
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [self] in
lcOptionsTVHeight.constant = tableView.contentSize.height
}
}
}
but this is not working in first load, but updating as i do scroll up and down, how can do load in properly in first load itself?

heightForRowAt(:)
You need to pass your height in heightForRowAt(:)
In this method, you need to check if it's the nested tableview cell and return the height, in other cases just return UITableView.automaticDimension
My suggestion would be to use sections instead of nested tableview. Only if there's horizontal scrolling requirement(not the case here as you mentioned tableview inside a tableview cell), we can go with a collectionview inside tableview cell.
Here, using different sections might be sufficient. You can reload sections by tableView.reloadSections
Again it might be dependent on your requirement. In general, we don't see tableview inside a tableview cell.

Related

Tableview methods not working when tableview scrolling disabled

Hy Stack family
I am facing the issue. I put tableview in scroll view with some other subviews. I am making an food ordering app. In which when i scroll up so my collection view will automatically scroll according to my tableview section or either i just select my category from collection view and i want to scroll my tableview at that position so my app user will select that product to order.
Plz help me out how can i handle tableview delegate methods when my main scrollview scrolling!
func tableView(_ tableView: UITableView,
willDisplay cell: UITableViewCell,
forRowAt indexPath: IndexPath) {}
This method is not working for me when i do scrolling!
1.check your delegate img 1 img 2 img 3
check your numberRows DataSource return count values (count !=0 )
check height of cell

Tableview Controller with a dynamic tableview in its static cell

I have a form inside a tableview controller. The last 3 parts of the form are address textfield, map and a save button. When the user begins typing on the address field a uitableview will slide up covering the map to display different results. When I set the child tableview mapTableview's delegate and datasource to self this is when the problem would start, the screen just displays a white background. I tried different solutions but they don't work. Also tried this one but the data source must be coming from the tableview controller itself. Dynamic Tableview inside a Static tableview Cell
When I create an array of strings in the class Datasource and put the following codes below in my tableview controller, the strings get displayed in the mapTableView.
var dataSource = DataSource()
mapTableView.delegate = dataSource
maptTableView.delegate = dataSource
But since the data source must be coming from tableview controller, I tried to put the code below, and many other things as suggested in other posts, in my table view controller but the screen won't display anything, just all white. And I get these errors:
UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window)
Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead
extension EditProfileTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return pois.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellResult", for: indexPath) as! MapTableViewCell
let poi = pois[indexPath.row]
cell.textLabel?.text = poi.title
cell.detailTextLabel?.text = poi.subtitle
cell.detailTextLabel?.numberOfLines = 0
return cell
}
}
and in my viewDidLoad
tableView.estimatedRowHeight = 60.0;
tableView.rowHeight = UITableView.automaticDimension;
How to correctly put a tableview inside a static tableview cell? Please help
You should create another table view controller for the inner table view. Let's call it MapTableViewController.
Add Container View into the static cell in storyboard.
Add Table View Controller into that container view, set its class to MapTableViewController.
Put all datasource/delegate methods for mapTableView from the EditProfileTableViewController into MapTableViewController. After that, EditProfileTableViewController should have no UITableViewDataSource and UITableViewDelegate protocol methods at all.

UIStackView in UITableView cell causes jerking when scrolled

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.

UITableview Height set according to cells

I have UITableView in UIViewController in this view controller total 3 tableview or 2 UICollectionView. so in this one tableview contain many cells or cells data are not static its dynamic according to server. So, I create the table view height outlet & its set on cellforrowatindex function of tableview.
So, we use to set height tableview_height.constant=tableview_height.constant+cell.frame.size.height using this code my problem showing whitespace on the end of UITableViewCell. If I increase the cell then cell are cut from the bottom. I am giving below constraint show in the pic.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if indexPath.row == 0
{
tableview_height.constant = 0
tableview_height.constant=tableview_height.constant+cell.frame.size.height
}
else
{
tableview_height.constant=tableview_height.constant+cell.frame.size.height
}
return cell
}
Check my answer on the almost same question.
How to increase the height of parent view according to height of UITableView in swift?
It is the best way to achieve what you want, because it does not depend on your table settings or if you have header/footer/custom insets/whatever.
Hope it will help.

Dynamic UITableView height in embedded view

I have a table view as part of a larger controller. This table view can have various number of rows and rows are expandable (e.g. unselected state is 50pt, selected -> 120pt).
To animate sell expanding/collapsing I use those methods:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.beginUpdates()
tableView.endUpdates()
view.setNeedsLayout()
view.layoutIfNeeded()
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
tableView.beginUpdates()
tableView.endUpdates()
view.setNeedsLayout()
view.layoutIfNeeded()
}
But I need to update container view height in parent view controller together with the table view.
I've tried to achieve that by adding observer to the contentSize of the tableView and assigning its height to the containerView height constraint. Its kinda work but animation of the containerView height happens after cell increase/decrease animation already happened.
Is there a way to achieve dynamic height table view in embedded view controller?
PS. I don't want to use table view footer/header to put surrounding views there as surrounding view structure is more complex and table view is just part of it.

Resources