Insted of scroll view i used Tableview controller, inside Tableview controller i desined by adding collection view, textfield also i want to add table view inside it.
I implemeted but it crashes the app beacuse of parent table view. Any solution?
Create a custom cell with tableView(Autoheight TableView) inside it. Add cell to your TableViewController.
Custom Cell with AutoHeightTableView
Set isScrolling, bounces of AutoHeightTable to false
Register custom cell to TableViewController
Load custom cell table data
final class AutoHeightTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
override var contentSize: CGSize {
didSet{
UIView.performWithoutAnimation {
self.invalidateIntrinsicContentSize()
}
}
}
}
Related
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()
}
}
}
}
I have created nested tableView approach, In a view controller I set tableView and then nested tableView for the Cell.
So its like Parent tableView and Child tableView.
For adjusting content height tableView row height autoDimension I have created custom class for it and override intrinsicContentSize but I am still getting scrollbar in nested tableview. ContentSize updating but row height not adjusting. I have already set automatic row height for both the parent and child table view.
How can I avoid this scrollbar when my content expand it should auto set tableView row height?.
Attaching GitHub code reference and images as well.
https://github.com/MasamMahmood/autoHeightTableView
Custom Class Code:
class MyOwnTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
print("ContentSizee \(contentSize)")
return self.contentSize
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
Images:
I'm trying to use a UITableViewController and allow the user to reorganize my data by dragging the rows. This is working fine, except that the user has to tap on the relatively small UITableViewCellReorderControl on the right quarter of the cell to drag the row.
Here's an example:
As far as I can tell there's no way to get ahold of the UITableViewCellReorderControl or access its size or behavior through the UITableViewDelegate. Is there a way to customize drag behavior so that you can drag anywhere on the cell to move it up or down?
just expand the reorderControl to the size of cell.
extension UITableViewCell {
var reoderControl: UIView? {
for view in self.subviews {
if view.description.contains("UITableViewCellReorderControl") {
return view
}
}
return nil
}
}
class Cell: UITableViewCell {
override func layoutSubviews() {
super.layoutSubviews()
reoderControl?.frame = bounds
}
}
I'm working on a project in Swift 3.0 and it has a UITableView inside a UIScrollView that has already been set its constraints.
The requirement is to display a list of songs that would return from an array in this UITableView, but the size of this UITableView should dynamically arrange according to the size of the array, so I do not need to scroll the UITableView since I can see all the songs in the UITableView (this UITableView is inside my scrollView, so instead I can use my scrollView do move up or down the list).
How would I achieve this?
You can change height of the tableview based on the content.
var frame = tableView.frame
frame.size.height = tableView.contentSize.height
tableView.frame = frame
If you are using autolayout create outlet for tableview height and assign tableview content size to its constant.
tableviewHeightConstraint.constant = tableview.contentSize.height
1.Create a subclass for tableView and override intrinsicContentSize.
class MyOwnTableView: UITableView {
override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return self.contentSize
}
override var contentSize: CGSize {
didSet{
self.invalidateIntrinsicContentSize()
}
}
override func reloadData() {
super.reloadData()
self.invalidateIntrinsicContentSize()
}
}
2. In Interface builder change the class of your tableView to MyOwnTableView (subclass of UITableView).
Set automatic row height for the table view.
tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension
I have in my View a long UIScrollView (about 1000 px in height), and at the end of this UIScrollView I have a UITableView.
The cellForRowAtIndexPath is never called (surely i checked the delegate and datasource if they are connected right, and the IBOutlet of the table is strong) but numberOfRowsInSection is getting called.
I tried reloading the TableView when the UIScrollView scrolls so when the table is at focus the cellForRowAtIndexPath might get called, but with no luck.
Did anyone encounter a similar behaviour when trying to use tableview and scrollview together?
Your hierarchy is like this:
A parentView is there. Inside the parent view there is a scroll view and there is a table view. So, your tableview is somewhere at 1000 from origin of parentview.
So, tableview will never become visible to your parentview and no delegates will be fired.
Include your view as a headerView of your UITableView like that:
Update for 2020, swift 5.X. To create a custom UITableView that allows your tableview inside a scrollview!
1) Create a subclass of UITableView:
import UIKit
final class ContentSizedTableView: UITableView {
override var contentSize:CGSize {
didSet {
invalidateIntrinsicContentSize()
}
}
override var intrinsicContentSize: CGSize {
layoutIfNeeded()
return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height)
}
}
2) Add a UITableView to your layout and set constraints on all sides. Set the class of it to ContentSizedTableView.
3) You should see some errors, because Storyboard doesn't take our subclass' intrinsicContentSize into account. At runtime it will use the override in our ContentSizedTableView class