11

我目前在使用UITableViewDiffableDataSource.

我想试一试这个新功能,所以我在网上看了很多教程,但似乎没有一个能回答我的问题。

在我当前的 viewController 中,我有一个UITableView, 有 3 个不同的对象(每个对象都有不同的类型),但是UITableViewDiffableDataSource是一个强类型。

喜欢:dataSource = UITableViewDiffableDataSource <SectionType, ItemType>

我所有的部分都充满了类似的东西

func numberOfSections(in tableView: UITableView) -> Int {
    return 3
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if section == 0 {        
        return bigObject.ObjectsOfType1.count
    } else if section == 1 {
        return bigObject.ObjectsOfType2.count
    } else {
        return bigObject.ObjectsOfType3.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! CustomTableViewCell
    if indexPath.section == 0 {
        cell.buildWithFirstObject(obj: bigObject.ObjectsOfType1[indexPath.row])
    } else if indexPath.section == 1 {
        cell.buildWithFirstObject(obj: bigObject.ObjectsOfType2[indexPath.row])
    } else {
        cell.buildWithFirstObject(obj: bigObject.ObjecstOfType3[indexPath.row])
    }
}

在我的情况下使用 diffable dataSource 有技巧吗?

任何帮助表示赞赏!谢谢你读我:)

4

3 回答 3

11

另一种可能会阻止转换NSObject为您期望的任何内容(这可能容易崩溃),是将您的不同对象作为关联值包装在enum符合Hashable. 然后,在你的出队回调中,你得到枚举,并且可以解开相关的值。所以像

enum Wrapper: Hashable {
  case one(Type1)
  case two(Type2)
  case three(Type3)
}

dataSource = UITableViewDiffableDataSource <SectionType, Wrapper>(collectionView: collectionView!) { [weak self] (collectionView: UICollectionView, indexPath: IndexPath, wrapper: Wrapper) -> UICollectionViewCell? in
  switch wrapper {
    case .one(let object):
      guard let cell = dequeueReusableCell( ... ) as? YourCellType else { fatalError() }
      // configure the cell
      cell.prop1 = object.prop1
      return cell

    case .two(let object2):
      guard let cell = dequeueReusableCell( ... ) as? YourCellType2 else { fatalError() }
      // configure the cell
      cell.prop1 = object2.prop1
      return cell

    case .three(let object3):
      guard let cell = dequeueReusableCell( ... ) as? YourCellType3 else { fatalError() }
      // configure the cell
      cell.prop1 = object3.prop1
      return cell
  }
}

您甚至可以使用单个return.

于 2020-01-17T15:19:20.647 回答
4

对于最简单的方法,使用AnyHashable项目标识符和enum节标识符。已接受答案的问题在于,当您向表中添加功能层时,它会增加繁重的复杂性,因为您必须始终以通用enum类型而不是自定义类型开始和结束,并且在您知道它之前到处都有开关。这种代码根本不读我们。而且我真的很讨厌看到我的数据源从自定义类型的数组变为相同泛型的数组enum

而且,与其他人所说的相反,您不能使用Hashable泛型类型,因为您不能使用协议本身来符合自身,这就是为什么AnyHashable存在“类型擦除”替代品的原因。

private enum Section {
    case title
    case kiwi
    case mango
}

private struct Title: Hashable {}

private struct Kiwi: Hashable {
    let identifier = UUID().uuidString
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(identifier)
    }
    
    static func == (lhs: Kiwi, rhs: Kiwi) -> Bool {
        return lhs.identifier == rhs.identifier
    }
}

private struct Mango: Hashable {
    let identifier = UUID().uuidString
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(identifier)
    }
    
    static func == (lhs: Mango, rhs: Mango) -> Bool {
        return lhs.identifier == rhs.identifier
    }
}

var dataSource: UITableViewDiffableDataSource<Section, AnyHashable>!

dataSource = UITableViewDiffableDataSource(tableView: tableView,
                                           cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in
    switch item {
    case is Title:
        return TitleCell()
        
    case let item as Kiwi:
        let cell = tableView.dequeueReusableCell(withIdentifier: someIdentifier,
                                                 for: indexPath) as? SomeCell
        cell?.label.text = item.identifier
        return cell
        
    case let item as Mango:
        let cell = tableView.dequeueReusableCell(withIdentifier: someIdentifier,
                                                 for: indexPath) as? AnotherCell
        cell?.label.text = item.identifier
        return cell
    default:
        return nil
    }
})

var initialSnapshot = NSDiffableDataSourceSnapshot<Section, AnyHashable>()
initialSnapshot.appendSections([.title, .kiwi, .mango])
initialSnapshot.appendItems([Title()], toSection: .title)
initialSnapshot.appendItems([k1, k2, k3], toSection: .kiwi)
initialSnapshot.appendItems([m1, m2, m3], toSection: .mango)
self.dataSource.apply(initialSnapshot, animatingDifferences: false)
于 2021-12-07T02:34:14.567 回答
1

似乎使用UITableViewDiffableDataSource<Section, NSObject>和让我的不同对象继承自NSObject工作正常。

于 2019-11-27T14:51:41.600 回答