Latest 3.2.4
Homepage https://github.com/sadawi/PrimarySource
License MIT
Platforms ios 9.0, osx 10.11
Authors

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

Latest podspec

{
    "name": "PrimarySource",
    "version": "3.2.4",
    "summary": "Collection data sources",
    "homepage": "https://github.com/sadawi/PrimarySource",
    "license": "MIT",
    "authors": {
        "Sam Williams": "[email protected]"
    },
    "source": {
        "git": "https://github.com/sadawi/PrimarySource.git",
        "tag": "3.2.4"
    },
    "source_files": "Pod/shared/**/*.{swift}",
    "ios": {
        "source_files": "Pod/iOS/**/*.{swift}"
    },
    "platforms": {
        "ios": "9.0",
        "osx": "10.11"
    },
    "osx": {
        "source_files": "Pod/OSX/**/*.{swift}"
    },
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This