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

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.

Related

uitableviewcell with dynamic height child view controller

I have a custom uitableviewcell that has an embeded child view controller. This child view controller uses autolayout and contains another uitableview. I'm trying to render the cell so that the child view controller's tableview is exactly the height of the contents.
I know that you can do that by setting tableview height constraint equal to the contentsize.height
but since it's in a tableviewcell, there is already an encapsulated height calculated. How do I force the cell to resize with the new child view controller's uitableview updated height constraint?
Try to Use AutoHeight TableView inside your custom cell instead of child View Controller.
Set scrolling and jumping property to false of AutoHeight Table.
final class AutoHeightTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
override var contentSize: CGSize {
didSet{
UIView.performWithoutAnimation {
self.invalidateIntrinsicContentSize()
}
}
}
}

How to Dynamically increase height of Two Tableview in a Scrollview

I have an issue at the moment I am trying to put two table views inside a scrollview in one controller and these tableview are placed one below another. these two tableview uses scrollview for scrolling.
so I used vertical stackview inside scrollview. but when I create cell, both tableview height is not increases as well as scrollview is not able to scroll.
How should i use scrollview scroll for scrolling tableview?
-- scrollview
-----VerticalStackView
--------Tableview 1
--------Tableview 2
I'm really lost with this.Any help will be greatly appreciate it.
You need to make each UITableView define it's own size based on their content. To do that subclass both of them using the class below.
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
Then, for each UITableView you need to set isScrollEnabled = false. Otherwise their defined size will be 0.
Then just add each table view to the stack view you're using inside the scrollview. If their combined height is larger than the screen height, it'll scroll.

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

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

Scroll vertical collection view along with whole view

I have two collection views as seen on the image on the link. I want such that when I scroll the vertical collection view up, the other views together with the horizontal collection view on top of it should scroll together. How can I do this?
The above image shows two collection views, the one on top is a horizontal collection view while the one on the bottom is a vertical collection view
You can do something like in the code snippet I just provided...
Implement the scroll view delegate method... and based on the collection view scrolled, set the content offset of the other one as per your calculations...
let horizontalCollectionView = UICollectionView()
let verticalCollectionView = UICollectionView()
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == horizontalCollectionView {
// set the content off set of the vertical collection view
} else if scrollView == verticalCollectionView {
// Set the content off set of horizontal collection view
}
}

How to make tableView cell "stick" while scrolling

I have a SegmentedControl on a Cell at the top of my tableView (the tableView consists of all static cells)...I want the top cell (and segmentedControl) to "stick" so that it is always visible as a user scrolls through the table (see screenshot below). Is this possible?
Usually, for applying this behavior you should add it as a header view instead of a cell. Header views in table view does "stick". But since you are adding static cells (UITableViewController), even if you tried to add a header view to the table view it won't apply the sticking behavior.
As a workaround you might need to add a new UIViewController contains a view on the top (the header view) and a container view, as follows:
The one hackish solution which pops to my mind is:
Declare header in your VC, or hold the reference:
private var header: UIView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 40))
Add it as a subview of tableView and set content inset equal to height of the header.
tableView.addSubview(header)
tableView.contentInset.top = header.frame.height
If your header use autolayout you will need to call
view.layoutIfNeeded() before setting content inset
and then for header to stick always to the top of tableView:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
header.frame.origin.y = scrollView.contentOffset.y
}

Resources