TableViewRow

public class TableViewRow<Cell> : CellProvider where Cell : UITableViewCell, Cell : Configurable, Cell : Reusable
extension TableViewRow: Hashable, Equatable where Cell.Model: Hashable & Equatable

The concept of a generic TableViewCell:

  • it knows about model it operates with
  • contains tap closure that is called when a cell is tapped
  • responsible for dequeueing a cell
  • provides methods to add leading / trailing actions

Example

  • Define a Cell Model
  struct StringCellModel:  Hashable  {
      let uniqueID: String
      let text: String

      // Defines uniqueness of the model
      func hash(into hasher: inout Hasher) {
          hasher.combine(uniqueID)
      }

      // Defines dynamic context of the model
      static func == (lhs: Self, rhs: Self) -> Bool {
          lhs.text == rhs.text
      }
  }
  • Define a Cell
  final class StringCell: UITableViewCell, ReusableCell, Configurable {
      typealias Model = StringCellModel
      let checkmarkView = UIView()

      func configure(with model: StringCellModel) {
          textLabel?.text = "ID: \(model.uniqueID): \(model.text)"
          selectionStyle = .none
      }

      override var isSelected: Bool {
          didSet {
              debugPrint(isSelected)
          }
      }

      override func prepareForReuse() {
          accessoryType = .none
      }

      override func setSelected(_ selected: Bool, animated: Bool) {

          if selected {
             accessoryType = .checkmark
          } else {
             accessoryType = .none
          }
          super.setSelected(selected, animated: animated)

      }
  }
  • create a TableViewRow
  let model = StringCellModel(uniqueID: ID, text: title)
  let row = TableViewRow<StringCell>(model: model,
                                     onTap: { debugPrint("tapped with \($0)")})
  // add rows if need
  row.addRowLeadingActions(leadingActions)
  row.addRowTrailingActions(trailingActions)

Available where Cell.Model: Hashable & Equatable

  • Conformance to Equatable protocol.

    Note: Two rows are considered equal if their models are equal

    Declaration

    Swift

    public static func == (lhs: TableViewRow, rhs: TableViewRow) -> Bool

    Parameters

    lhs

    TableViewRow<Cell>

    rhs

    TableViewRow<Cell>

    Return Value

    Bool

  • Conformance to Hashable uses hash of the model

    Declaration

    Swift

    public func hash(into hasher: inout Hasher)

    Parameters

    hasher

    Hasher