English 中文(简体)
不同用途的电离辐射
原标题:UITableViewDiffableDataSource with different objects

我目前正在使用<代码> 可调用的电文处理麻烦。

我要对这一新特点表示枪声,因此,我对这个网络进行了许多辅导,但似乎没有人回答我的问题。

我目前的看法 主计长有<代码>可调取的,有3个不同的物体(各有不同类型),但<可调用的电文/代码>则严格分类。

同:dataSource = UITableViewDiffableDataSource <sectionType, 项目Type>

All my sections are fed with something like

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

在我的案件中是否有使用可分散数据的方法?

任何帮助都值得赞赏! 感谢您阅读我:

最佳回答

关于最简单的做法,我建议使用“AnyHashable>>:> 标书,作为项目识别标志和“条形”栏目。 我将避免对项目识别特征进行点数,因为它能够迅速摆脱手头,经常使用开关单,把通用的点数输入习惯类型,而这种分类不仅过于烦.,而且根本看不上去。

// MARK: SECTION IDENTIFIERS

private enum Section {
    case title
    case kiwis
    case mangos
}

// MARK: DATA 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
    }
})

// MARK: USAGE

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)
问题回答

另一种可能性,即防止将<代码>NSObject投给您所期望(可能易坠毁)的任何东西,即把您的不同物体列为符合<代码>Hashable的<编码>的相关数值。 之后,在你们的退步中,你获得了 en,能够消除相关的价值。 类似情况

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

You could probably even simplify that with a single return.

看来,使用<代码>可调取的DataSource< Section, NSObject>,且我的不同物体继承NSObject





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签