FTMTableSectionModules 0.7.0

FTMTableSectionModules 0.7.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Oct 2019
SPMSupports SPM

Maintained by Franciso Javier Trujillo Mata.



  • By
  • Francisco Javier Trujillo Mata

FTMTableSectionModules

Build Status Version License Platform Readme Score

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Is valid for iOS 7 and higher. Requires Swift 5.0 and XCode 8.0 or higher.

Installation

FTMTableSectionModules is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'FTMTableSectionModules'

How to use it

FTMTableSectionModules basically is a pack of tools that is helping you to develop faster. This libabry is so useful for those UIViewControllers that are based in UITableView. The main concept in this libary is, a Module means a Section in a UITableView, so there is a subclass of UIViewController called ModulesViewController that manage all modules.

A Module is a like and mini UIViewController, should be able to work it self.

Basically a ModulesViewController has an array of TableSectionModules.

Sometimes an example is easier to understand than 1000 words.. so, please to see the example use:

pod  try 'FTMTableSectionModules'

If you don't have pod try installed go to https://github.com/CocoaPods/cocoapods-try to install it.

Inside you will see an example of a ViewController with 2 differents modules and a couple of cells per module.

In case that you still want to see here the examples, let's give a try.

Let me give it a try

1. Create a Module

You need to create a subclass of TableSectionModule

class FirstSectionModule: TableSectionModule {}

2. Override the needed methods in the Module

There are a lot of methods that could be override. The most usuals are:

  • Registration of UITableViewCell/UITableViewHeaderFooterView with Class/Nib
override func registerClassForCells() -> [AnyClass]
override func registerClassForHeadersFooters() -> [AnyClass]
override func registerNibsForCells() -> [AnyClass] 
override func registerNibsForHeadersFooters() -> [AnyClass] 
  • Creation of rows, this is the like the data source of the UITableView
override func createRows()
  • Dequeue and configure of the UITableViewCell
override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell
  • Rest of method that could be override You can override basically the same method that the UITableViewDelegate and UITableViewDataSource offer.

  • Obviously you will need to create&configure all the UITableViewCells that the Module would contains.

Example of TableSectionModule with methods

import FTMTableSectionModules

class FirstSectionModule: TableSectionModule {
    override func registerNibsForCells() -> [AnyClass] {
        return super.registerNibsForCells() + [
            Example1TableViewCell.classForCoder(),
        ]
    }
    
    override func registerClassForCells() -> [AnyClass] {
        return super.registerClassForCells() + [UITableViewCell.classForCoder()]
    }
    
    override func createRows() {
        super.createRows()
        
        rows.append(String(describing: Example1TableViewCell.self))
        rows.append(String(describing: UITableViewCell.self))
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: className, for: indexPath)
        
        let className = rows[(indexPath as NSIndexPath).row] as! String
        //Addtional configuration for the cell
        switch className {
        case String(describing: UITableViewCell.self):
            cell.textLabel?.text = "A tottally native cell"
            break
        default:
            break
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
        return 44.0
}



3. Create a ModulesViewController

You need to create a subclass of ModulesViewController

class MyViewController: ModulesViewController {}

4. Override the needed methods in the ViewController

The methods to be override in the ModulesViewController are less than on the Modules. Usually is just needed to override the createModules

override func createModules()

This method will add all the modules that the view controller could have. Like this example:

override func createModules() {
        super.createModules()
        
        appendModule(FirstSectionModule(tableView: tableView!))
    }

Example of ModulesViewController with methods

import FTMTableSectionModules

class MyViewController: ModulesViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 44
        
        tableView?.tableFooterView = UIView()
    }

    override func createModules() {
        super.createModules()
        
        appendModule(FirstSectionModule(tableView: tableView!))
    }

}




As could appreciate this is a very good approach to avoid masive view controllers. The main idea is to split responsabilities.

  • A ModulesViewController will manage (add/remove) TableSectionModule
  • A TableSectionModule will manage the cells that the section itself will contains
  • A TableSectionModule could contain enough logic & responsability how to make it fully work that section of the UITableView



Enjoy and be module my developer! :godmode:



Author

Francisco Javier Trujillo Mata, [email protected]

License

FTMTableSectionModules is available under the MIT license. See the LICENSE file for more info.