具有不同对象的 UITableViewDiffableDataSource

Ale*_*det 6 ios swift diffabledatasource

我目前在使用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])
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

ofl*_*hra 9

另一种可能会阻止强制转换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
  }
}
Run Code Online (Sandbox Code Playgroud)

你甚至可以用一个return.


bso*_*sod 7

对于最简单的方法,使用AnyHashable表示项目标识符,使用 anenum表示部分标识符。

接受的答案的问题在于,当您向表中添加功能层时,它会增加大量的复杂性,因为您必须始终以通用类型enum而不是自定义类型开始和结束,并且在您意识到之前,到处都有开关。这很快就会变成意大利面条式代码。

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

// MARK: Section identifiers

private enum Section {
    case title
    case kiwis
    case mangos // or is it mangoes? the debate continues...
}

// MARK: Models

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

// MARK: Data source

private 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: KiwiCell.identifer,
                                                 for: indexPath) as? KiwiCell
        cell?.label.text = item.identifier
        return cell
        
    case let item as Mango:
        let cell = tableView.dequeueReusableCell(withIdentifier: MangoCell.identifier,
                                                 for: indexPath) as? MangoCell
        cell?.label.text = item.identifier
        return cell
    default:
        return nil
    }
})

var initialSnapshot = NSDiffableDataSourceSnapshot<Section, AnyHashable>()
initialSnapshot.appendSections([.title, .kiwis, .mangos])
initialSnapshot.appendItems([Title()], toSection: .title)
initialSnapshot.appendItems([k1, k2, k3], toSection: .kiwis)
initialSnapshot.appendItems([m1, m2, m3], toSection: .mangos)
self.dataSource.apply(initialSnapshot, animatingDifferences: false)
Run Code Online (Sandbox Code Playgroud)