Ameba Ownd

アプリで簡単、無料ホームページ作成

Enric Macias Lopez Portfolio

Expandable Table View Controller

2016.07.12 02:38

In this post I would like to talk about a library that I developed one year ago and I updated recently to its version 2.0. The library is named ExpandableTableViewController.

This ExpandableTableViewController is about a regular UITableView that adds nested subrows to the regular UITableView rows. On top of that, aforementioned subrows can be shown and hidden (hidden by default) at any time by clicking on their mother row. 

In other words, if a normal UITableView has sections and rows, an ExpandableTableView has sections, rows and subrows.

As the above images show, using this strategy it can help us to show information in a really engaging style. Showing determined kinds of information only when necessary. And because of the subrows can be created and edited as a normal UITableViewCell, they can show any kind of information that can be displayed in a normal cell. It should also be noted that we are talking about subrows and not a single subrow, that is to say that inside a row we can add as many subrows as we need.

The library also implements a special index path to easily arrange sections, rows and subrows. This object is called ExpandableIndexPath.

ExpandableIndexPath

public struct ExpandableIndexPath {
       public var section = 0
       public var row = 0

       public var subRow = 0

}

Using this resource object let us to handle subrows as easy as handling sections and rows in a regular UITableView. This ExpandableIndexPath is created in order to be used along with the ExpandableTableViewDelegate and the ExpandableTableView. Both classes implement similar methods as a regular UITableView, but handling an ExpandableIndexPath instead of a NSIndexPath. Below you can see the declaration methods of each class.

ExpandableTableViewDelegate

Rows

func expandableTableView(expandableTableView: ExpandableTableView, numberOfRowsInSection section: Int) -> Int

func expandableTableView(expandableTableView: ExpandableTableView, cellForRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> UITableViewCell

func expandableTableView(expandableTableView: ExpandableTableView, heightForRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> CGFloat

func expandableTableView(expandableTableView: ExpandableTableView, estimatedHeightForRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> CGFloat


func expandableTableView(expandableTableView: ExpandableTableView, didSelectRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath)

Subrows

func expandableTableView(expandableTableView: ExpandableTableView, numberOfSubRowsInRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> Int

func expandableTableView(expandableTableView: ExpandableTableView, subCellForRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> UITableViewCell

func expandableTableView(expandableTableView: ExpandableTableView, heightForSubRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> CGFloat

func expandableTableView(expandableTableView: ExpandableTableView, estimatedHeightForSubRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath) -> CGFloat


func expandableTableView(expandableTableView: ExpandableTableView, didSelectSubRowAtExpandableIndexPath expandableIndexPath: ExpandableIndexPath)

ExpandableTableView

// Returns the cell at the ExpandableIndexPath given.
public func cellForRowAtIndexPath(expandableIndexPath: ExpandableIndexPath) -> UITableViewCell?

// Deques a cell a the ExpandableIndexPath given.

public func dequeueReusableCellWithIdentifier(identifier: String, forIndexPath expandableIndexPath: ExpandableIndexPath) -> UITableViewCell


// Shows if the ExpandableIndexPath provided is an expanded cell or not.

public func isCellExpandedAtExpandableIndexPath(expandableIndexPath:ExpandableIndexPath) -> Bool

// Deselects the cell at the ExpandableIndexPath given.

public func deselectRowAtExpandableIndexPath(expandableIndexPath: ExpandableIndexPath, animated: Bool)

To sum up ExpandableTableViewController is a Swift library that easily lets us show, hide and customize table view cells as an expandable list of items.

For those who want to know more about this library, you can browse code, download and have a detailed explanation on how to install the library in here: https://github.com/enricmacias/ExpandableTableViewController

キケ