首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用UICollectionViewDiffableDataSource时处理moveItem(at:to:)委托方法

使用UICollectionViewDiffableDataSource时处理moveItem(at:to:)委托方法
EN

Stack Overflow用户
提问于 2020-03-10 13:27:41
回答 1查看 225关注 0票数 2

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

在使用委托时,它是这样工作的:

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

以这种方式设置数据源不起作用

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-10 13:40:30

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

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

https://stackoverflow.com/questions/60611991

复制
相关文章

相似问题

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