【问题标题】:Handle moveItem(at:to:) delegate method when using UICollectionViewDiffableDataSource使用 UICollectionViewDiffableDataSource 时处理 moveItem(at:to:) 委托方法
【发布时间】:2020-03-10 05:27:40
【问题描述】:

我需要使用moveItem(at:to:) 委托方法来移动我的集合视图单元格。 当我实现数据源委托方法时,它工作正常,但我需要将我的数据源设置为UICollectionViewDiffableDataSource,这样moveItem(at:to:) 就没有调用。 任何解决方案,

这种方式在使用委托时可以工作:


override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return 10
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
        cell.textLabel.text = "Some value"
        return cell
}

override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
        true
}

override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
        print("OK")
}

这样设置数据源是行不通的


func setupDataSource() {
        dataSource = UICollectionViewDiffableDataSource<Section, Person>(collectionView: collectionView, cellProvider: {
            (collectionView: UICollectionView, indexPath: IndexPath, person: Person) -> UICollectionViewCell? in
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
            cell.textLabel.text = "Some text"
            return cell
        })
    }

【问题讨论】:

    标签: ios swift uicollectionview ios13 swift5


    【解决方案1】:

    对于自定义行为,创建UICollectionViewDiffableDataSource 的子类并覆盖那里的方法。

    【讨论】:

      猜你喜欢
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      相关资源
      最近更新 更多