CocoaPods trunk is moving to be read-only. Read more on the blog, there are 18 months to go.

PrimarySource 3.2.4

PrimarySource 3.2.4

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Mar 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Sam Williams.



  • By
  • Sam Williams

PrimarySource

Overview

This is a Swift library for setting up data sources for UITableViews and UICollectionViews.

Usage

Setting up the data source

Create a DataSource object and set it as your UITableView/UICollectionView delegate and dataSource. A DataSource contains Sections, and each section contains items. Items are responsible for configuring cells, which may have been reused.

You’ll probably want a method to rebuild the data source whenever you get new data:

func loadData()
    self.dataSource = DataSource()
    self.tableView.delegate = self.dataSource
    self.tableView.dataSource = self.dataSource

    self.dataSource <<< Section(title: "Form") { section in
        section <<< CollectionItem<TextFieldCell> { cell in
            cell.title = "Name"
            cell.onChange = { [unowned cell] in
                print("New value: \(cell.value)")
            }
        }
    }

    self.tableView.reloadData()
}

Cell registration

A cell can be created in several ways:

  • By class (instantiated purely from code)
section <<< CollectionItem<TextFieldCell>()
  • By storyboard identifier (using dynamic prototypes from the storyboard)
section <<< CollectionItem<TextFieldCell>(storyboardIdentifier: "NormalTextCell")
  • From a nib
section <<< CollectionItem<TextFieldCell>(nibName: "NormalTextCellNib")

Cell types

A number of cell types are included.

Display cells:

  • TableCell
  • SubtitleCell
  • ActivityIndicatorCell

Interactive cells:

  • ButtonCell

Field cells (each with a field and a strongly typed value):

  • TextFieldCell (UITextField, String)
  • SwitchCell (UISwitch, Boolean)
  • DateFieldCell (UIDatePicker, NSDate)
  • PushSelectCell (typed set of options, selectable in a pushed view controller)
  • EmailAddressCell (String)
  • PhoneNumberCell (String)
  • IntegerCell (Int)
  • PasswordCell (String)
  • StepperCell (UIStepper, Int)
  • SliderCell (UISlider, Float)

Cell action handlers

Handlers can be added for actions like deleting, tapping, and reordering cells. Adding a handler will enable the corresponding action in the UI: swipe to delete will become possible, the cells will highlight on tap, and the reordering accessory views will appear in edit mode.

Handlers can be added for cell-level actions:

  • onDelete
  • onTap
section <<< TableViewItem<TextFieldCell> { cell in
    cell.title = "Name"
    // ...
}.onTap { _ in
    print("tapped") 
}.onDelete { _ in 
    print("deleted") 
}

And section-level actions:

  • onReorder
dataSource <<< Section { section in
    // ...
}.onReorder { sourceIndexPath, destinationIndexPath in 
    // ...
}

TODO

  • support for UICollectionViews