This article post shares the best knowledge about customizing TableView cell with dynamic size in Swift. iPhone app developers India have intended this post to help new developers to know an easy way to customize table view cell. Follow his tutorial to know more.
One-liner: In this tutorial, we create a custom table view cell with dynamic size. This tutorial is implemented in Xcode 8 and built for iOS 10.
Start by creating one single view application with product name CustomTableCellWithDynamicSize. In the language field select swift language and devices field select universal.
First, go to File New File iOS Cocoa Touch Class and add class DynamicCellwith subclass of UITableViewCell. It shows in below screenshot.
Start with configuring storyboard, first open Main.storyboard and drag UITableView from Object Library. Ctrl + drag from table view to ViewController.swift class and declare IBOutlet as tblView. Select a prototype cell and set custom class as DynamicCell, also set a cell identifier as CellIdentifier. After that drag two labels from Object Library and add into prototype cell with name Title and Description. Ctrl + drag from labels to DynamicCell.swift class and declare IBOutlet as lblTitle and lblDescription respectively.Check this in below screenshots.
In the DynamicCell.swift class, there are no changes to be done and will look like the below screenshot.
Now open ViewController.swift class and set UITableViewDelgate and UItableViewDataSource. Declare one array with some manual element like Name and Description that you want to show in the output. Check below screenshot.
In viewDidLoad() make changes as shown in below screenshot for table view auto re-size and fit all text into the label.
Next methods are UITableViewDataSorce and Delegate. In numberOfItemInSection() method we set total number of array count. In cellForRowAtIndexPath() method we declare object of DynamicCelland set reusable identifier. Also, set labels text from an array. Check below screenshot for more info.
Now build and run the project. If everything is correct, your app should run properly, and you will see below output. You can see that table view cell resize as per content length.
The purpose of iPhone app developers India was to make you learn how to customize TabelView cell with dynamic size in swift. You should follow every instruction shared by experts for the best results. If you have any confusion or question, ask in comments below.