HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何使用RxSwift和RxSwiftDataSources将表视图与表示不同数据类型的多个部分绑定在一起?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用Rx Swift创建一个包含多个部分的表视图.每个部分显示代表不同类型的数据.

我找到了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)) ])
]

我现在想要修改示例,只想在第二部分中显示字符串而不是CustomData的实例,所以有点像这样:

let sections = [
  SectionOfCustomData(header: "First section",SectionOfString(header: "Second section",items: ["a","b","c"])
]

这显然不会编译,因为段现在包含不同类型的元素SectionOfCustomData和SectionOfString.我试图通过尝试将部分声明为[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
  }
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何使用RxSwift和RxSwiftDataSources将表视图与表示不同数据类型的多个部分绑定在一起?全部内容,希望文章能够帮你解决ios – 如何使用RxSwift和RxSwiftDataSources将表视图与表示不同数据类型的多个部分绑定在一起?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。