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 easy way to customize tabelview 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 is built for iOS 10.
Start by creating one single view application with product name CustomTableCellWithDynamicSize. In 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. This is shown 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 prototype cell and set custom class as DynamicCell, also set 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 DynamicCell.swift class there are no changes to be done and will look like 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 output. Check below screenshot.
In viewDidLoad() make changes as show in below screenshot for table view auto re-size and fit all text into 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 array. Check below screenshot for more info.
Now build and run 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 best results. If you have any confusion or question, ask in comments below.