# API Reference The `DataSourceController` provides an API to dinamically insert, update or remove individual rows and sections. Most of this API is declared by the protocols `RowManager` and `SectionManager`. In addition, there are several additional helper functions and properties implemented in the main class. ## Helper functions and properties ```swift weak var delegate: DataSourceControllerDelegate? { get set } ``` Returns or sets the `DataSourceController` delegate property, as described in the [DataSourceControllerDelegate](README.md#datasourcecontrollerdelegate) section of the README, which is notified of the changes in rows and sections of the controller. It's optional, and can be set either during or after the initialization. -------------------------------------------------- ```swift var totalRowCount: Int { get } ``` Returns the total number of rows currently present in all the `Section` instances of this `DataSourceController` instance. Note that the number of sections is usually not 0 as per implementation, since every initializer does create at least one `Section` instance and the only way to reduce this number to 0 is to manually call functions to remove them. Therefore this is the property to check for "emptyness". -------------------------------------------------- ```swift func modelObject(at indexPath: IndexPath) -> Any? ``` Returns the model object instance at the selected `indexPath`. If the `indexPath` is out of bounds on either the `section` or the `row` properties, this function returns `nil` without crashing. -------------------------------------------------- ```swift func data(for section: Int) -> SectionDataModelType? ``` Returns the `SectionDataModelType` conforming object for the selected `section` index. If no section data has been provided or `section` is out of bounds, this function returns `nil` without crashing. -------------------------------------------------- ```swift func register(dataController: CellDataController.Type, for model: Any.Type) ``` Registers the `CellDataController` conforming type to be used with the `model` specific type. A few notes on this: * The same controller can be used safely with different model objects. * Only a single controller type may be registered to a given model object type. Successive calls to this function with the same model object type will result in any previous registered controller type being overwritten. ## RowManager protocol ```swift func add(row modelObject: Any, at indexPath: IndexPath, notify: Bool) ``` Inserts a new row at the given `indexPath`. There are several different outcomes depending on the `indexPath` passed as a parameter. * If `indexPath.section` is out of bounds, a new `Section` with a single row is inserted at the end of the `Section` array. * If `indexPath.section` is within bounds, but `indexPath.row` is out of bounds, a new row is inserted at the end of the given `Section`. * If both `indexPath.section` and `indexPath.row` are within bounds, the new row is inserted at the specified position of the given `Section`, moving "down" the rows behind the newly inserted one. If `notify` is `true`, the following code is executed: ```swift delegate?.dataSourceWasMutated(self, section: indexPath.section) ``` -------------------------------------------------- ```swift func update(row modelObject: Any, at indexPath: IndexPath, notify: Bool) ``` Updates the model object at the given `indexPath`. There are several different outcomes depending on the `indexPath` passed as a parameter. * If either `indexPath.section` or `indexPath.row` are out of bounds, the function returns without performing any further action. No notification function in the delegate will be called. * If both `indexPath.section` and `indexPath.row` are within bounds, the model object at the given `indexPath` is replaced by the new instance `modelObject` passed as a parameter. If `notify` is `true`, the following code is executed: ```swift delegate?.dataSourceWasMutated(self, rows: [indexPath]) ``` -------------------------------------------------- ```swift func update(rows: [Any], atSection index: Int, notify: Bool) ``` Replaces all the model objects at the `Section` at position `index` by the `rows` passed as a parameter. * If `atSection` is out of bounds, the function returns without performing any further action. No notification function in the delegate will be called. * If `atSection` is within bounds, the value in the `rows` property of that `Section` instance is replaced by the new value `rows` passed to the function. **Note:** Only the model objects in the rows are replaced, whilst section data remains unmodified. If `notify` is `true`, the following code is executed: ```swift delegate?.dataSourceWasMutated(self, rows: (0..