Create dynamic peak UITableViewCell in Swift
On this tutorial, we’ll learn to create dynamic peak UITableViewCell in Swift. To realize dynamic peak 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 peak routinely. The one situation is that you simply arrange your cell constraints accurately. So allow us to study, how we will create dynamic peak UITableViewCell in UITableView utilizing swift.
Steps to create the multi line UILable in UITableViewCell of the UITableView
1: Create a single view utility and identify it “Multi-Line-UILable”. After creating the undertaking you will notice following information contained by the undertaking (i’m mentioning solely 3 information right here)
- AppDelegate.swift
- ViewController.swift
- Principal.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 identify it “MultilineLabelCell”.
4: Open MultilineLabelCell.xib and observe the steps as proven within the under picture sequence. Right here constraints will play necessary position in figuring out UITableViewCell peak, as a result of autolayout at all times take into account the constraint throughout rendering of the content material. In our case the cell will at all times have 11.5 pixel area from backside of the cell. Although, you possibly can change fixed worth of backside constraint to see the completely different situations.
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 peak 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 identify in viewdidload. By now you might be seeing error in your xcode, that’s as a result of we haven’t but applied the delagets and datasource for uITableView.
Step 8: Under is the whole code that may produce dynamic cell peak for UITableView row, as per the content material displayed by UILabel.
Run your code and you will notice, cell of dynamic peak based mostly on content material size.