Skip to content

Repository files navigation

MultiSelectHeaders

A simple and robust implementation of a multi-select section header along with selectable cells, for use in iOS table views.

Walkthrough

Requirements

  • Xcode 14 or later
  • Swift 5
  • iOS 13.0+

Just change the Models to suit your needs

In the example, section headers are populated by LeagueModel and cells are populated by TeamModel. The important point when adapting this to your own data is that the header model (LeagueModel) owns the array of cell models it contains (teamsArr is an array of TeamModel).

Architecture

  • TeamModel / LeagueModel — plain, UIKit-free value types with proper initializers (including a dictionary initializer for JSON-style data) and synthesized Equatable/Hashable conformance.
  • MultiSelectionModel — a pure, UIKit-independent type that owns all selection state. It tracks selection by position (section, row) so two equal teams in different sections are handled independently, and it treats a section header as selected only when every row in that section is selected. Extracting this logic out of the view controller is what makes the behaviour unit-testable.
  • MultiApprovalController — the table view controller. It is now a thin layer that forwards taps to MultiSelectionModel and reloads the table.

Running the tests

The MultiSelectHeaderTests target contains unit tests for the models and the selection logic. Run them from Xcode with Cmd+U, or from the command line:

xcodebuild test \
  -project MultiSelectHeader.xcodeproj \
  -scheme MultiSelectHeader \
  -destination 'platform=iOS Simulator,name=iPhone 15'

(Use any simulator you have installed for the -destination value.)

About

A simple and robust implementation of having a multi select header along with selectable cells for various implementation in iOS.

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages