簡體   English   中英

如何使用RxSwift和RxSwiftDataSources將表視圖與表示不同數據類型的多個部分綁定?

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

我正在嘗試使用RxSwift創建一個包含多個部分的表視圖。 每個部分顯示代表不同類型的數據。

我找到了RxSwiftDataSources庫並從他們的文檔中實現了這個例子。

以下是該示例如何實現的快速問題:

定義了自定義數據類型CustomData

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

然后,添加該部分的表示(請注意,此處實現了SectionModelType ):

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

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

最后,創建一些示例數據並將其綁定到表視圖:

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)) ])
]

我現在想修改示例,只想在第二部分顯示String而不是CustomData實例,所以有點像這樣:

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"])
]

這顯然不會編譯,因為sections現在包含不同類型的元素SectionOfCustomDataSectionOfString 我試圖通過嘗試將部分聲明為[SectionModelType]來解決這個問題,但這不起作用,編譯器抱怨:

協議' SectionModelType '只能用作通用約束,因為它具有Self或關聯類型要求

您可以使用枚舉來包裝不同類型。

使用枚舉,SectionOfCustomData定義應該是這樣的:

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)
    }
  }
}

configureCell看起來像這樣:

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
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM