How to resize custom cells in a TableView? - ios

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.

Related

How to give size to table view to XIB

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
}

Q: How can I change the cell size in UITableView, to show all the labels?

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!

How to show an image view only if the model object contains image url

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
}

Problems with autolayout in uitableviewcell

I am having some hard time as usual getting the constraints correct. This is how my tableview works:
This is the default display. The height of the cell is 57.
When you click on a cell, i change the selected cells height to 90 so it displays this information:
My problem is to set the constraints so it looks exactly as it does in both images. What is the best solution to this?
Would it be easier if i made two views and placed them inside of my uitableview so the constraints will be set for the specific view instead of the cell's frame which is being changed on user click etc. I uploaded my project if anybody wants a better overview of what is going on:
https://ufile.io/u2y5p
You can also put your information inside a UIStackView and make them invisible until you click on the cell, use the UITableViewAutomaticDimension to make the cell's height dynamic, so when you will make those information visible, reloading the UITableView will make your cell's height higher
Suppose you are displaying the data on TableView from an array of models. Keep a property in your model class named as "isExpanded" and set its value by default false.
Now, use below code:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let dataModel = self.arrayOfModels[indexPath.row]
return dataModel.isExpanded ? 100 : 50
}
On select a row:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let dataModel = self.arrayOfModels[indexPath.row]
dataModel.isExpanded = true
self.tableView.reloadRows(at: [indexPath], with: .automatic)
}
Let me know if you have any doubt, or need more clarification.

iOS UITableViewCell asynchronous image download and change UITableView height accordingly with the use of auto layout

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()

Resources