I am new to iOS practicing XIB I have created an XIB for a cell
Now When I resize it from here I automatically change its behaviour in tableview I want to give this XIB with as 100% of tableview. I actually want to set Width of this XIB as tableview
You will need to set the size in the UITableViewDelegate's heightForRowAt(_:) method.
For purposes of building UI in interface builder, you can just drag the corner into the size you want.
If you have your constraints setup correctly, the size you use to layout your cell in your XIB has no effect on the resulting use in a table view.
For example, with this layout:
I can stretch the frame like this:
or this:
or this:
or this:
and it will ALWAYS look like this when I run the app:
Try
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return tableView.frame.height
}
Related
I nearly finished making my tableview cell and all the contents in data model shows up in the UI so I think the connection is fine. The problem is that my prototype cell shows only part of what it's supposed to show. I have 3 labels in the cell, but it only shows one and a half. the text is stuck to the left(I wanted to put it in center), and the textlabel is cut off so it doesn't show in the UI.
It's supposed to look like this.
I tried
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableView.automaticDimension
this code in the ViewController to make my cell to automatically set height to my labels, but it didn't work.
I also used vertical stack view and made constraints so it fills the cell fully, and it doesn't seem to make a change.
Please help!
You have to use implement the UITableViewDelegate protocol and the function
func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat
where you can return the custom height for each cell
You can do this in the Storyboard - you can just drag a cell in the UITableView to be bigger. The UI of resizing the table view cell is the same as resizing any UI element in the Storyboard. The rest of them will be too, though. Hope this helps!
I am trying to create an homepage like instagram for iPhone, in the storyboard I have set the 'post cell' using a UITableViewCell and anchoring it using AutoLayout. The cell in the storyboard looks like this:
As you can see the height is fixed to 555 pixel but when I start the simulator the cells of the UITableView don't have the height I fixed in the storyboard size inspector. It looks like this:
How can I fix this issue?
If you want to set fix height then you can set in controller also.
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 555
}
It may be Help you :)
So I am creating a facebook feed of sorts and I am fetching text, url for images/videos and username etc. The cell I have created has an image view, text view and label. I am sizing the cell according to the text view content size.
Now lets say the user has only posted text and there no image with it. I want the cell to be sized so that there is no image view in the cell. Only the text view and thats it.
Can this be done using just one cell/model/adapter or do I need custom cells and show them according to the model.
A little light on how this could be done would be appreciated.
You just need one cell and there are different ways of achieving this. A simple oen is using a UIStackView inside the cell.
When one element of the UIStackView is hidden, it adapts it size. If you place constraints from to the stack view to the cell, the cell with be smaller/larger depending if you show the image. You just have to work with constraints.
Also, using UIStackView or not, remember to specify on your tableView that the size of the cell will be dynamic by doing:
self.tableView.rowHeight = UITableViewAutomaticDimension
Create a outlet of image height constraint, and then check if your URL is blank or invalid then make height constraint constant 0 otherwise give it appropriate height.
And make sure you have implemented both the methods :
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100; // Put estimated height
}
And another one is,
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
I am learning iOS development and Swift. I am trying to create a simple to-do app, with custom cells in a TableView. I have created the custom cell with a XIB file. In this, the cell looks like this:
However, when I run the app on the simulator, it looks like this:
Where am I going wrong and how can I Fix it?
You can fix the cell height to a default value by putting something like
self.tableView.rowHeight = 44.0
In your viewDidLoad, where tableView corresponds to the IBOutlet that is your UITableView in your xib file.
If you would like to do this dynamically e.g. by calculating the size of the cell's contents and then resizing the cell so it fits nice and snug, you should implement
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 44 // The cell's height
}
Be sure to set your UITableView's delegate to self to use this last method.
Please see this tutorial for more info.
I'm writing an application where images are downloaded asynchronously and when the images are loaded its scaled but I just want to show as the same size within the UITableViewCell.
Since i'm using auto layout I have tried to change the height constraint through an IB but then I have to change the height of the cell and I though of reloading each cell after downloading each image but not sure if it's a good idea.
Its like the Facebook application where images are loaded and the uitableviewcell is dynamically changed.
Have you tried this delegate method?:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
// return downloadedimage.bounds.height etc.
}
Also do not forget to reload data in the viewDidLoad method
tableView.reloadData()