Create dynamic top UITableViewCell in Swift
On this tutorial, we are going to discover ways to create dynamic top UITableViewCell in Swift. To realize dynamic top we are going to create multi line UILabel in UITableView utilizing swift language.
We’ll use the brand new UITableViewAutomaticDimension property. UITableViewAutomaticDimension property is a useful factor for iOS builders because it units the cell top mechanically. The one situation is that you just arrange your cell constraints appropriately. 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 identify it “Multi-Line-UILable”. After creating the venture you will note following recordsdata contained by the venture (i’m mentioning solely 3 recordsdata right here)
- AppDelegate.swift
- ViewController.swift
- Important.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 comply with the steps as proven within the beneath picture sequence. Right here constraints will play vital function in figuring out UITableViewCell top, 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 house from backside of the cell. Although, you’ll be able to change fixed worth of backside constraint to see the totally 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 beneath 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 identify in viewdidload. By now you might be 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: Under is the whole code that can produce dynamic cell top for UITableView row, as per the content material displayed by UILabel.
Run your code and you will note, cell of dynamic top primarily based on content material size.