Create dynamic top UITableViewCell in Swift
On this tutorial, we’ll discover ways to create dynamic top UITableViewCell in Swift. To realize dynamic top we’ll create multi line UILabel in UITableView utilizing swift language.
We are going to use the brand new UITableViewAutomaticDimension property. UITableViewAutomaticDimension property is a useful factor for iOS builders because it units the cell top routinely. The one situation is that you simply arrange your cell constraints accurately. So allow us to study, how we will create dynamic top UITableViewCell in UITableView utilizing swift.
Steps to create the multi line UILable in UITableViewCell of the UITableView
1: Create a single view software and title it “Multi-Line-UILable”. After creating the challenge you will notice following information contained by the challenge (i’m mentioning solely 3 information right here)
- AppDelegate.swift
- ViewController.swift
- Fundamental.Storyboard
2: Open ViewController.swift and create an IBOutLet for UITableView.
3: Create a brand new file which is a base class of UITableViewCell class and title it “MultilineLabelCell”.
4: Open MultilineLabelCell.xib and comply with the steps as proven within the under picture sequence. Right here constraints will play essential position in figuring out UITableViewCell top, as a result of autolayout all the time contemplate the constraint throughout rendering of the content material. In our case the cell will all the time have 11.5 pixel area from backside of the cell. Although, you’ll be able to change fixed worth of backside constraint to see the completely different eventualities.
5: Open MultilineLabelCell.swift and create IBOutlet for the UILabel.
6: Open MultilineLabelCell.xib and join IBOutlet lblTitle to UILabel.
7: Open ViewController.swift and add under code.
Within the above code, first we created IBOutlet for UITableView and named it as tblList. Secondly, we set delegate and datasource for our tblList UITbaleView. Subsequent we have to inform the default estimated row top in order that it improved efficiency as defined by APPLE. Lastly telling our ViewController.swift class that we’re going to implement delegate and datasource for UITableView.
As we’re utilizing customized class so we have to register customized cell xib title in viewdidload. By now you’re seeing error in your xcode, that’s as a result of we haven’t but carried out the delagets and datasource for uITableView.
Step 8: Beneath is the entire code that may produce dynamic cell top for UITableView row, as per the content material displayed by UILabel.
Run your code and you will notice, cell of dynamic top based mostly on content material size.