Search
Sunday 22 October 2017
  • :
  • :

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 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.

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 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.

2

3

In DynamicCell.swift class there are no changes to be done and will look like 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 output. Check below screenshot.

5

In viewDidLoad() make changes as show in below screenshot for table view auto re-size and fit all text into 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 array. Check below screenshot for more info.

7

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.

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 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+.