Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind table view with multiple sections that represent different data types using RxSwift and RxSwiftDataSources?

I am trying to create a table view with multiple sections using RxSwift. Each section displays data representing a different type.

I found the RxSwiftDataSources library and implemented the example from their documentation.

Here is a quick runthrough of how that example is implemented:

A custom data type CustomData is defined:

struct CustomData {
  var anInt: Int
  var aString: String
  var aCGPoint: CGPoint
}

Then, the representations for the section are added (note that SectionModelType is implemented here):

struct SectionOfCustomData {
  var header: String    
  var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
  typealias Item = CustomData

   init(original: SectionOfCustomData, items: [Item]) {
    self = original
    self.items = items
  } 
}

Finally, some sample data is created and bound to the table view:

let sections: [SectionOfCustomData] = [
  SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
  SectionOfCustomData(header: "Second section", items: [CustomData(anInt: 2, aString: "two", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 3, aString: "three", aCGPoint: CGPoint(x: 3, y: 3)) ])
]

I now want to modify the example and only want to display Strings rather than instances of CustomData in the second section, so somewhat like this:

let sections = [
  SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
  SectionOfString(header: "Second section", items: ["a", "b", "c"])
]

This will obviously not compile as sections now contains elements of different types SectionOfCustomData an SectionOfString. I tried to work around this by trying to declare sections as [SectionModelType] but this doesn't work, the compiler complains with:

Protocol 'SectionModelType' can only be used as a generic constraint because it has Self or associated type requirements

like image 433
nburk Avatar asked Nov 06 '16 23:11

nburk


1 Answers

You can use enums for wrapping different types.

Using enum, SectionOfCustomData definition should be like this:

enum SectionOfCustomData: SectionModelType {

  typealias Item = Row

  case customDataSection(header: String, items: [Row])
  case stringSection(header: String, items: [Row])

  enum Row {
    case customData(customData: CustomData) // wrapping CustomData to Row type
    case string(string: String)             // wrapping String to Row type
  }

  // followings are not directly related to this topic, but represents how to conform to SectionModelType
  var items: [Row] {
    switch self {
    case .customDataSection(_, let items):
      return items

    case .stringSection(_, let items):
      return items
    }
  }

  public init(original: SectionOfCustomData, items: [Row]) {
    switch self {
    case .customDataSection(let header, _):
      self = .customDataSection(header: header, items: items)

    case .stringSection(let header, _):
      self = .stringSection(header: header, items: items)
    }
  }
}

And configureCell looks like this:

let dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>()
...
dataSource.configureCell = { [weak self] (dataSource, tableView, indexPath, row) -> UITableViewCell in

  switch dataSource[indexPath] {
  case .customData(let customData):
    let cell: CustomDataCell = // dequeue cell
    self?.configure(cell: cell, with: customData)
    return cell

  case .string(let string):
    let cell: StringCell = // dequeue cell
    self?.configure(cell: cell, with: string)
    return cell
  }
}
like image 113
mitsuyoshi Avatar answered Oct 29 '22 05:10

mitsuyoshi