How to get tableview content height if tableview is in tabview cell? - ios

The table view cell has a tableview inside it. This tableview has sections and rows, when user clicks on section the rows shows or collapse. but I can not get the content height of this cell.
on section's button click I am doing this:
self.tableViewViewHeightConstraint.constant = CGFloat.greatestFiniteMagnitude;
self.tableView.reloadData()
self.tableView.layoutIfNeeded()
self.tableViewViewHeightConstraint.constant = self.tableView.contentSize.height
delegate?.reloadMainTable(with: self.indexPath)
after setting the constraints I am reloading this cell in main tableview with the delegate but not able to get a fixed height

Try to check contentView height of UITableviewCell
For more detail of contentView

Related

Programmatically Scroll at specific item or offset, with UIKit Compositional Layout collectionView?

I have an iOS 14 collectionView, configured with compositional layout.
You can scroll vertically through the collectionView, and you can scroll horizontally within the sections.
I would like to know if there is a way to scroll programmatically, within a section?
Something like collectionView.setContent(offset: 100, inSection: 2)
And is there a way to get the contentOffset of a particular section?
I can't find any function to do that.
Sincerely,
Jery
Its a hack but for such sections the parent is a UIScrollView which you can get a reference to by referencing a cell in the section and then referencing its superview.
Like this:
let cell = collectionView.cellForItem(at: indexPath)!
let scrollView = cell.superview as! UIScrollView
scrollView.setContentOffset(<#CGPoint#>, animated: true)

How to animate tableview cells from bottom to top

I have a tableview which grows and shrink on user click. When user lands to the screen the height of tableview is same as cell(single cell). On click of that cell multiple cells get populated and tableview height would be same as no. Of cells. I want have animation from last cell to first cell, but what I am getting is animation from first cell to last cell, my tableview is at bottom of the screen(which grows in upper direction)
This is my code inside will display method
cell.alpha = 0
UIView.animate(with
duration:0.4,
delay:0.05*Double
(
indexPath.row),animation: {
cell.alpha = 1
}
According to my understanding, you want a tableView to scroll from bottom to top.
// scroll from bottom to top with delay
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
tableView.setContentOffset(.zero, animated: true)
}

Sending heightForRow:atIndexPath height value after calculating it in-cell

I have a UITableView that has few cells with different heights. Each row's is dynamically adjusted according to the content it contains.
I have a custom UITableViewCell with a UICollectionView in it that should be resized according to how many cells it has (all the cells should be visible, without inside scrolling).
I also have a UITableViewCell with another UITableView that should be resized according to how many cells it has (all the cells should be visible, without inside scrolling).
The problem is that I don't have the contentSize of the collectionView and the tableView while heightForRow:atIndexPath gets called so I can't set the values to something right.
I've tried to set it to UITableViewAutomaticDimension and to set the cell.contentView.frame.size.height to the contentSize when it got set (added an observer on "contentSize") but then the cells were on top of each other (the collectionView was ontop of the tableView instead of above it).
The tableView code is a regular tableView code.
The collectionView code is:
self.collectionView.collectionViewLayout = UICollectionViewLeftAlignedLayout()
let collectionViewFlowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
collectionViewFlowLayout.estimatedItemSize = CGSize(width: 34, height: 50)
What is the best way to adjust the size of the tableView and the collectionView?
Thank you!
You can just ask UITableView to adjust its cell height again with this piece of code:
self.tableView.beginUpdates()
self.tableView.endUpdates()

Resizing a cell which contains a tableview with dynamic content in it

I have a Tableview inside a Table View cell.
The Tableview has a accordian view that can expand or collapse.
How to resize the cell to the tableview height which changes dynamically?
I would use a delegate.
Create a protocol that your outer table view controller conforms to:
func cellHeightChanged(newHeight: CGFloat) {
self.cellHeight = newHeight
self.tableView.reloadData()
}
Then give the inner table view a reference to the outer controller and call this method whenever the height changes.

Dynamic height TableView in a Scroll View

I am designing a page having a scroll view and above it a table view(scroll disabled). For doing this I have referred answers in this question - Make UITableView not scrollable and adjust height to accommodate all cells ,but wasn't successful.
Hierarchy of views along with provided constraints-
-Main View
-Scroll view
pinned to all sides of main view(0,0,0,0), constraint to margins
-Content View
pinned to scroll view(0,0,0,0),equal width to main view,equal height to main view(priority - 250)
-Table view inside content view
scroll disabled,having 50 point spaces from all sides,Height(>=),bottom spacing 50(relation >=).I have put greater than equal so as to increase height dynamically.
Now when I populate my table view I use the code as
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableview.dequeueReusableCellWithIdentifier("cellreuse", forIndexPath: indexPath)
cell.textLabel?.text = name[indexPath.row]
tableview.frame.size = tableview.contentSize
return cell
}
So when I run my code, it increases the tableview frame but doesn't stretch the content size and it just becomes weird as my scroll view doesn't scroll to the end of the table view neither my table view obeys the auto layout constraints.
Just I needed to do this -
remove the line - tableView.frame.size = tableView.contentSize
Add a height constraint for table view.
Set priority to High
Create an outlet of the height constraint(Ctrl+Drag).
Wherever you need to reload data of your table, set the height constraint to tableview's content height.
tableHeightConstraint.constant = tableview.contentSize.height
Assign a table height. Let it be constant 0.
Just add below lines.
tableView.heightConstant.constant = CGFloat.greatestFiniteMagnitude
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.heightConstant.constant = tableView.contentSize.height
With this, you can easily achieve dynamic table height. Working on iOS 13, Swift 5.
Had the same issue and resolved it by doing the following:
Create an outlet of the height constraint for the table view with a priority of 1000
#IBOutlet private weak var tableViewHeight: NSLayoutConstraint!
On viewDidLayoutSubview call layoutIfNeeded on the table view and then set the table view height constraint to the height of the content view
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
tableView.layoutIfNeeded()
tableViewHeight.constant = tableView.contentSize.height
}
Tested on iOS 14.1 and iOS 16.1

Resources