Search
Tuesday 12 November 2019
  • :
  • :

Custom TableView Cell With Dynamic Size in swift

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.

1

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.

2

3

In the DynamicCell.swift class, there are no changes to be done and will look like the below screenshot.

4

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.

5

In viewDidLoad() make changes as shown in below screenshot for table view auto re-size and fit all text into the label.

6

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.

7

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.

8

9

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.



Vijay is a compulsive blogger who likes to educate like-minded people on various new technologies and trends. He works with Aegis SoftTech as a software developer and has been developing software for years. Stay Connected to him on Facebook and Google+.