A simple and robust implementation of a multi-select section header along with selectable cells, for use in iOS table views.
- Xcode 14 or later
- Swift 5
- iOS 13.0+
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).
TeamModel/LeagueModel— plain, UIKit-free value types with proper initializers (including a dictionary initializer for JSON-style data) and synthesizedEquatable/Hashableconformance.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 toMultiSelectionModeland reloads the table.
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.)
