首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同对象的UITableViewDiffableDataSource

不同对象的UITableViewDiffableDataSource
EN

Stack Overflow用户
提问于 2019-11-27 14:06:08
回答 3查看 3.6K关注 0票数 12

我目前正面临使用UITableViewDiffableDataSource时遇到的问题。

我想尝试一下这个新特性,所以我在网上上了很多教程,但是他们似乎都没有回答我的问题。

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

喜欢:dataSource = UITableViewDiffableDataSource <SectionType, ItemType>

我所有的部分都充满了这样的东西

代码语言:javascript
复制
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])
    }
}

在我的情况下,是否有一个使用可扩散dataSource的技巧?

任何帮助都是非常感谢的!(谢谢你读我的文章:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-17 15:19:20

另一种可能是防止将NSObject转换到您所期望的任何东西(可能容易崩溃),那就是将不同的对象包装成符合Hashableenum中的关联值。然后,在去队列回调中,您将得到枚举,并可以打开相关的值。所以就像

代码语言:javascript
复制
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来简化这个过程。

票数 12
EN

Stack Overflow用户

发布于 2021-12-07 02:34:14

对于最简单的方法,项目标识符使用AnyHashable,节标识符使用enum

可接受的答案的问题是,在向表中添加功能层时,它增加了大量的复杂性,因为您必须始终以通用enum (而不是自定义类型)开始和结束,而且在您知道它之前,到处都有开关。这可以很快成为意大利面代码,这是要避免的!

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

代码语言:javascript
复制
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)
票数 5
EN

Stack Overflow用户

发布于 2019-11-27 14:51:41

使用UITableViewDiffableDataSource<Section, NSObject>和让我的不同对象继承NSObject似乎很好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59071993

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档