I added a UIView above a tableview to make it scrollable and have some tableviewcell below it but I can't figure out how to set "Detail Header" view's height dynamic so it fits to my content. I have a textLabel inside which increases its height because of this
All constraints inside the View are fine but somehow that UIView doesn't adjust to content and it keeps its fixed size
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return detailHeader
}
As per my understanding of your question - how to set a table view header height dynamically -
at viewDidLoad method, please use below lines:
tableView.estimatedSectionHeaderHeight = 120
//open var sectionHeaderHeight: CGFloat(default is UITableViewAutomaticDimension)
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
override viewForHeaderInSection and use Auto Layout to constrain elements in your view as seen fit. That's done.
"doesn't allow me to put images yet" what you mean? As per my understanding if you are using Storyboard and Drag and Drop Image so it will assign an image straight forward using image option on the right pane.
This is late... but I had this exact problem with setting the height of a regular UIView above table view cells inside of a Table View.
I solved it by creating a reference to this top UIView in the view controller holding the table view. Then I just set the height to it dynamically based on the my criteria.
Example code:
topView.frame.size.height = 50
tableView.layoutIfNeeded()
I've been googling an answer for this for hours but cannot find anything that actually works in my case and I've no idea why.
I'm creating a tableview which should be from the top of the tableview to the bottom of the tableview, but in this case it doesn't.
img here
In this image you can see how it looks like in the storyboard (Which is also the way I want it to look like), you can see it almost cover the whole tableview.
And this is the result:
result of tableview
I'm trying to make the tableview cell start from the top and contain all the way to the bottom of the tableview (Well, like the storyboard image)
I've added this...
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return 410
}
You dont need to use UITableView
You add vertical scrollview and add one contianer UIView in it then you can add mutiple UILable and UITextField one bellow another.
Evening,
I have the following design:
red box: is a table row
orane box: is a collection view
blue box: is a collection cell
When a collection view is empty I would like the row, or the collection view to autoresize to 0.
Currently I got this:
So I want the orange box autoresizing to height = 0 when there aren't blue box.
I know that I should use a constraint of collectionView Height and set to 0 when is empty.
But i have the outlet in the table cell and the collection delegate into the view controller. And I don't know how to accomplish this.
Any tips? With some code example please :P
I don't think it's possible to resize collectionView depending on its content automatically.
What you can actually do is add constraint of height to your CollectionView and set it to 0 if it's empty.
Then, your cell will have intrinsic height, if you connect CollectionView bottom and top to it accordingly (which you possibly already done)
The last step will be to set tableView.rowHeight to UITableViewAutomaticDimension. Probably it will work :)
Try to use
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
function. In this function, for the corresponding row, return 0.
I have just created an app and have started hooking up #IBOutlet's to the storyboard. I am connecting some of them to labels in a UITableViewCell Prototype Cell with a Basic Style. When I connect it though I get this error in the Storyboard:
The detailText Outlet from the TableViewController to the UILabel is invalid. Outlets cannot be connected to repeating content.
Can someone help me out? I have set it up the way I always do successfully but this time it has chucked me this error.
Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.
There are two types of table views cells provided to you through the storyboard, they are Dynamic Prototypes and Static Cells
1. Dynamic Prototypes
From the name, this type of cell is generated dynamically. They are controlled through your code, not the storyboard. With help of table view's delegate and data source, you can specify the number of cells, heights of cells, prototype of cells programmatically.
When you drag a cell to your table view, you are declaring a prototype of cells. You can then create any amount of cells base on this prototype and add them to the table view through cellForRow method, programmatically. The advantage of this is that you only need to define 1 prototype instead of creating each and every cell with all views added to them by yourself (See static cell).
So in this case, you cannot connect UI elements on cell prototype to your view controller. You will have only one view controller object initiated, but you may have many cell objects initiated and added to your table view. It doesn't make sense to connect cell prototype to view controller because you cannot control multiple cells with one view controller connection. And you will get an error if you do so.
To fix this problem, you need to connect your prototype label to a UITableViewCell object. A UITableViewCell is also a prototype of cells and you can initiate as many cell objects as you want, each of them is then connected to a view that is generated from your storyboard table cell prototype.
Finally, in your cellForRow method, create the custom cell from the UITableViewCell class, and do fun stuff with the label
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourCell
cell.label.text = "it works!"
return cell
}
2. Static Cells
On the other hand, static cells are indeed configured though storyboard. You have to drag UI elements to each and every cell to create them. You will be controlling cell numbers, heights, etc from the storyboard. In this case, you will see a table view that is exactly the same from your phone compared with what you created from the storyboard. Static cells are more often used for setting page, which the cells do not change a lot.
To control UI elements for a static cell, you will indeed need to connect them directly to your view controller, and set them up.
If you're using a table view to display Settings and other options (like the built-in Settings app does), then you can set your Table View Content to Static Cells under the Attributes Inspector. Also, to do this, you must embedded your Table View in a UITableViewController instance.
Or you don't have to use IBOutlet to refer to the object in the view. You can give the Label in the tableViewCell a Tag value, for example set the Tag to 123 (this can be done by the attributes inspector). Then you can access the label by
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "someID", for: indexPath)
let label = cell.viewWithTag(123) as! UILabel //refer the label by Tag
switch indexPath.row {
case 0:
label.text = "Hello World!"
default:
label.text = "Default"
}
return cell
}
With me I have a UIViewcontroller, and into it I have a tableview with a custom cell on it. I map my outlet of UILabel into UItableviewcell to the UIViewController then got the error.
As most people have pointed out that subclassing UITableViewCell solves this issue.
But the reason this not allowed because the prototype cell(UITableViewCell) is defined by Apple and you cannot add any of your own outlets to it.
Sometimes Xcode could not control over correctly cell outlet connection.
Somehow my current cell’s label/button has connected another cell
I just remove those and error goes away.
For collectionView :
solution:
From viewcontroller, kindly remove the IBoutlet of colllectionviewcell
. the issue mentions the invalid of your IBOutlet. so remove all subclass which has multi-outlet(invalids) and reconnect it.
The answer is already mentioned in another question for collectionviewcell
Click on simulator ,
Navigate to Window and enable Device Bezels
I am creating an application with a Table view controller. I am following this tutorial link. Now my problem I want to change the height inside of the items. Each cell has two labels vertically. I set height for the row like this.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath == selectedIndexPath ){
return 200
}else {
return 100
}
}
How can I change the height of items inside of cell.
Edit 1:
In default label has only one line of text after clicked there is multiple line of text. So need to change the height of the label and the whole cell.
Edit 2:
Before click my cell I have like this.
After I clicked my cell I want like this:
Make the items in the call scale dynamically depending on the bounds size of the cell.
You can configure layout constraints to achieve that. You would set constraints, define margins etc. The label would scale depending on the height.
Alternatively you can do the layout manually, calculating and setting the frames in layoutSubviews method of the cell.
Here is a nice tutorial that might help you Dynamic Table View Cell Height and Auto Layout