I have a custom UITableViewCell that its width is larger than UITableView's one (>1500px), user can scroll horizontally to show the whole cell.
I tried to set the contentSize.width of the table to match my cell's width but when I scroll the table view to the left, and when I reach a point higher than screen width, all information disappear and table become white.
You simply have to use a UIScrollView inside each UITableViewCell.
I ended up putting the table view inside UIScrollView and set its width to match maximum UITableViewCell width, then set the content width of the UIScrollView to match this width, now I can scroll the table horizontally without any problem, thanks.
Related
I have a tableView and a collectionView inside scrollView. Both of them have constraints for left, right, top and bottom. But both views are now visible when inside scrollView
Why may it be ?
I think scrollViews Need to know width and height for its subViews
Follow these steps:
Disable scroll for table view and collection view.
Resize the height of table view and collection view as per its content (If Dynamic height)
I have a UITableView cells that won't follow it's parent width. I found so many codes for how to change the height, however I couldn't found codes/ways to change UITableView cells to same as it parents.
Some of answer told that it would follow automatically. But in mine, the problem is the width can't follow device's width. Is there anyone that know how to change it?
The UITableviewCell width will always be the same width of the UITableView.
If you want to set the width of the cell same as the parent of UITableView, You should set the width of the UITableView same as that of parent
If you want to set the width of UITableView the same as it's parent view, set the trailing and leading constraints of your UITableView to it's superview with value 0
you need to remove the width constraint and aspect ratio that you have added to it. Instead of ctrl and drag, select the tableview and click the pin button and set the constraints as shown in image. Remember the trailing and leading values must be 0 for setting the width as same as parent view
I am trying to add table view that is contained into a view, this view has a width constraint and the height of the view will be dictated by the content height of the table view. However, I am unable to get this to work.
My attempt:
I create a view with a leading, top and width constraint. Then I insert a table view inside this view, this table view will have a leading, top, bottom and trailing constraint to the view (it's superview). I also set the row height and the estimate. When running the code, numberOfRowsInSection gets called but cellForRowAt does not, my guess is because the height didn't solve properly and the table has a height of 0. How can I fix this?
Thanks.
luis,
UITableView inherits from UIScrollView. So You cant expect the tableView to provide the height for its superView. In fact its vice versa.
TableView's frame will be decided by the superView's frame where as the tableView's contentSize will be decided by the data that you have provided.
So your best bets
Solution 1:
Add a height constraint to the View (SuperView) and create a IBOutlet to it in your VC.
Set the height for each cell in tableView lets say as 50.
Once you get the data to show in tableView before calling reloadData() on tableView set the constant of View's height constraint as
viewsHeightConstraint.constant = (numberOfRows) * 50;
yourView.layoutIfNeeded()
Solution 2:
If you are planning to use automatic height for cell then set the height of view based on the ContentSize of tableView. But to do that you have to get the data first, reload the tableView with the data and once done now get the content size of TableView and set the frame of your view accordingly.
I have added UICollectionView.I have also added the Cell in the UICollectionView.The issue is cell always getting space from top of 74.On interface builder the y space column always shows the disabled value please tell how can i remove such issue?
Any given cell's position is determined by the collection view. You can adjust the cell's insets to increase spacing between cells and the scroll view's insets to position all of the cells in the scroll view, you can't directly manipulate a cell's position out of the box. When noting the position of your first cell you should also take into account whether your viewController has extend edges under top bars on and whether it has adjust scroll view insets on.
I searched a lot for a solution but can't find anything to solve this particular case.
I created a custom cell in a UITableView, with these elements in order from top with the constraints:
imageview (constraints top screen, fixed width, fixed height, centered horizontal)
top-label (constraints top imageview, fixed width, centered horizontal) Line set to 0.
view (constraints top label, fixed width, centered horizontal) The height is set to 0.
button (constraints top view, fixed width, fixed height, centered horizontal)
bottom-label (constraints top view, constraints bottom screen, fixed width, fixed height, centered horizontal)
The cell is autosized in height correctly, also if I insert a text for top-label very long.
Now I want to attach an action to the button to enable the resize in height of the view, like an accordion. So I'm trying to change the height with no success, anything change also if I reload the tableview.
I tried to set a constraints to the height of the view, but If I change that all the content move but the height of the cell doesn't change.
The only way I can have the view changing the height is setting the the rowheight of the table, but that change all the cells of the table and I want to open the view of only one cell.
Is there another way to do that?
Thanks in advance
Ale
EDIT:
For clearance I've solved using the delegate method suggested by noobular, setting the view to 0 height permit to have that expanded when the height of the cell change.
I achieved a similar effect by these methods. First, I determine a constraint that will represent the height of my cell. I'm using the height constraint of a decorative bar on the left hand side that's pinned to the top and bottom of the cell. It's important that this constraint belong to an object that is a child of the cell's content view and that will be on-screen in both open and closed states. This constraint is connected to a property on the cell it will control.
When I want to change the height of the cell, i.e. to 100, I can do so as follows:
cell.heightConstraint.constant = 100.0
cell.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
The calls to beginUpdates and endUpdates force the tableView to layout its cells again, thus expanding or contracting the cell. Note that I'm actually calling the first two lines in the cell's own setSelected:animated: function and the remaining two in the table view controller's tableView:didSelectCellAtIndexPath: method. This has the effect of expanding a cell when it is selected.
Edit: For auto sizing cells where you want UIKit to automatically calculate the height, I would suggest researching UITableViewAutomaticDimension
Implement this method for your tableView delegate:
tableView:heightForRowAtIndexPath: