DSAutoSizeTableView 1.0.3

DSAutoSizeTableView 1.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Aug 2015
SPMSupports SPM

Maintained by Daniel Saidi.



AutoSizeTableView

AutoSizeTableView is an iOS Swift library that can resize resize table view cells to fit their content.

The problem

In iOS, a table view controller must be able to calculate the height (or at least return a value) for each cell. It may, however, be hard to do so if the cell height depends on the width, since the method is called before any cells have been displayed (and cells thus not yet have a frame).

To solve this problem, a common practice is to use a hard coded width. This is a bad approach, since a cell’s width depends on factors like device type, table view style and device orientation.

How AutoSizeTableView does it

Instead of using hard coded values, the AutoSizeTableView class hijacks the first reload method call and uses it to render fake content into a hidden table view.

As soon as this is done, the AutoSizeTableView class will have all the information it needs to be able to calculate the height of each cell in the real table view.

The first reload call is not noticable to the user and is immediately followed by a second reload method that makes use of the collected information.

How to use AutoSizeTableView

To use this functionality, make sure that your table view inherits AutoSizeTableView instead of UITableView. In heightForRowAtIndexPath then just cast the table view to an AutoSizeTableView instance and return the value of the heightForCell method.

If your app supports multiple orientations, implement the didRotateFromInterfaceOrientation method and call the handleOrientationChange method.

You can also have your table view controllers inherit the AutoSizeTableViewController class. It’ll take care of everything automatically. If inheritance is not an option, you can just grab the methods implemented in that class.

AutoSizeTableView automatically calculates the height for .Default and .Subtitle table view cells. Custom cells must implement AutoSizeTableViewCell.

Current issues

For now, you must manually set the number of lines to 0 for each cell label in the storyboard. If you don’t do this, the auto sizing will not work.

Demo

The demo project in this repository shows you how you can use the classes to auto size standard table view cells.

CocoaPod

You can easily add this library to your own project using CocoaPods (check out https://cocoapods.org). Just add the following to your pod file:

pod 'AutoSizeTableView', '<version>'

Contact

To get in touch with me regarding this and other projects that I am working on:

Project site:   http://github.com/danielsaidi/AutoSizeTableView
CocoaPod:       https://cocoapods.org/pods/AutoSizeTableView
Blog:           http://danielsaidi.wordpress.com
Twitter:        http://twitter.com/danielsaidi

Contributions to this project are more than welcome. Just send me a pull request or get in touch.