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
Related
I have created an expandable tableview by separated with multiple sections. Because this tableview is inside a container view, so I need to update the container view size every time the tableview expand or collapse.
I update the container size with tableview.contentsize.height but I found out the content size are different when its expanded or collapsed.
For e.g, lets say there are 4 sections, if I collapse the 3rd and 4th sections the tableview will return me height of 200. Meanwhile if I expand the table just 1st and 2nd section while 3rd and 4th is collapsed, the content size height is more than 200.
I find it very frustrated why the program is giving me such confusing information, because I need a proper content size height to update the container view. Anyone here can enlighten me?
Instead of using the contentSize try calculating the height on your own. It'll be much easier if every cell is the same height. Let's say they are all 50 height and there is 1 row in section 1 and 3 rows in section 2. (1 + 3) * 50 = container height. Obviously if you have sections and insets you will also need to include that into your calculation.
If your cells are different heights, just call the table views cell height delegate to get the relevant height.
If this doesn't work for you then please be more descriptive and add some code.
The behavior of content size that you've described is correct. If you want to know the height of the UITableView you should look at it's frame, not at it's content size.
I'm not sure why you need a container view for the table view, you can layout it like the simple UIView, then you'll have one less problem, just change the tableView frame.
Update:
After reading comments i see that you want tableView inside of tableViewCell. Most part you'll have to do yourself, but i can help you with UITableViewAutomaticDimension.
Try adding this to your viewController:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
override func tableView(tableView: UITableView, estimatedHeightForRowAt indexPath: NSIndexPath) -> CGFloat {
return 44//approximate cell height
}
The main thing is to set constraints properly in interface builder. And remember that constraints won't change the height of the cell on builder, you may have to do it yourself in the Size inspector. Anyway if you won't - it will be resized during the build.
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 4 components in my cell.
UIView
UITextView
UIImageView
Another UIView on top of the UIImageView
UIView
UIView
These content can be changed according the data that comes from the server(Sometimes it needs to hide the 3rd image view, sometimes 5th UIView). I'm using auto layout and with the auto layout what is the way to change height of the cell?
You can achieve this easily by using UIStackView. Stack views are a powerful tool for quickly and easily designing your user interfaces. Their attributes allow a high degree of control over how they lay out their arranged views.
Here is link for tutorial - Tutorial
If you hide a view from stackview it conveniently disappears from the layout, but it's still a subview of the stack view. So you don't have to do anything special if you want to bring it back later on. And by using self sizing cells, cells will automatically expand or collapse based on stack view height.
Do following steps:
When you hide any component give that component frame height as 0 and reload tableview. If you giving any component constant height then take outlet of height constraint and make it zero and specify its constant height again when you unhide.
When you unhide give him specific frame height and reload tableView.
heightForRow must returnUITableViewAutomaticDimension
As you already taken components from the storyboard so compiler understands the height of cell and work accordingly.
If you still facing issue you can ask.
this two methods are use for dynamic cell height
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I post this for other man meet my issue.
Do not apply any change that will change the cell height in layoutSubviews, like hidden view or change any view's height.
I have a TableView with 2 prototype cells in it. I want one of them to be static (call it header cell), and the other one to be dynamic (call it description cell).
So I set TableView cell to Dynamic Prototypes and 2 for prototypes cells). I have also set up the TableView and increased prototype cells; and put dummy data for seeing them working.
However, whatever I have done I couldn't resize the Header cell.
First, in Storyboard, I tried changing Row Height for the Header Cell (and I ticked the 'Custom'). It seems like changing the height of the row in the Storyboard; in fact it doesn't after the build.
Secondly, this width and height bit seems disabled.
It's also same for the Content View just under the Header Cell. Width & Height adjusting part is disabled.
By the way, everything is same for the Description Cell. I tried adding it a row height, and it seems like changing in the Storyboard, but after build, no luck. It is just behaving by itself.
I should also highlight that it's giving a bit more room for Description Cell which I return cell.textLabel?.text = "BlaBla" (so it has to write the text itself); but for the other cell where I am dragging/dropping on Storyboard, it just squeezes the height.
What may I be doing wrong? What do you think do I miss?
Edit: (I have a Navigation Bar at the top)
How it looks on Simulator:
How it looks on Storyboard:
As you can see it's narrowing the Header Cell (so messing the design), and increasing the height of Description Cell randomly.
If I can do it with heightForRowAtIndexPath, how should I use this approach for giving different height to 2 prototypes cells. (I want to have Header Cell to be static row height, but Description - Dynamic, or at least not identical height)
You're going to implement - tableView:heightForRowAtIndexPath: in your table view's delegate and return the height you'd like for each row.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return 44
}
How do I create a table view with static cells, where the row heights are all equal to make the set of static cells fill the table view?
The row height needs to be adjusted dynamically to account for the different iOS screen sizes.
For example I want a table view with 7 static cells of equal heights that fill the rest of the screen space except the navigation bar.
I've tired using the following with no luck:
self.tableView.rowHeight = self.tableView.frame.height/6;
and
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return self.tableView.frame.height/6;
}
and the autoLayout feature in storyboards is grayed out when I try to use it with any part of the table view.
Appreciate any help thanks!
Define auto layout constraints for your prototype cell.
Specify the estimatedRowHeight of your table view.
Set the rowHeight of your table view to UITableViewAutomaticDimension.
If we express the last two points in code, it looks like this.
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension