• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 将 Collection View 中的单元格移动到另一部分的最后一个单元格之外不起作用

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:01:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

为了能够在 Collection View 中移动/平移单元格,应该实现 UIGestureRecognizer。这在单节 Collection View 中工作正常。但是,在多节 Collection View 中,当一个单元格被平移/移动到另一个节的最后一个单元格之外时,会发生错误。 尽管“cellForItemAtIndexPath”在平移期间被正确触发以显示"new"单元格,但不应在平移超出最后一个可用单元格后触发(即在这种情况下 indexPath.row == numberOfItemsInSection 并导致错误(参见下面的代码)) .

在表格 View 中,(默认)移动/平移实现不会出现此问题。

这可能是 Swift 中的一个错误吗?是否有人建议在平移超出部分中的最后一个单元格时避免触发“cellForItemAtIndexPath”?

下面是 Swift 2.3 中的一个工作代码示例来说明问题。

class CustomViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    private var list = ["one", "two", "three","four","five","six","seven","eight"]
    private var itemsList: [[String]]!

    private var collectionView : UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        itemsList = [list, list, list] // Create three sections

        collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.translatesAutoresizingMaskIntoConstraints = false
        self.collectionView.registerClass(CustomCell.self, forCellWithReuseIdentifier: "cell")

        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(CustomViewController.handleGesture(_))
        self.collectionView.addGestureRecognizer(panGesture)
        self.view.addSubview(self.collectionView)
    }

    override func viewWillLayoutSubviews() {
        let margins = view.layoutMarginsGuide
        self.collectionView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor, constant: 0).active = true
        self.collectionView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 0).active = true
        self.collectionView.bottomAnchor.constraintEqualToAnchor(view.bottomAnchor, constant: 0).active = true
        self.collectionView.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 0).active = true
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int   {
        return itemsList.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
         return itemsList[section].count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCell

        let items = itemsList[indexPath.section]

        cell.label.text = items[indexPath.row] // The error occurs here, after panning beyond the last cell of another section stating: "fatal error: Index out of range"
        return cell
    }

    func collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
        return true
    }

    func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
        let itemToMove = itemsList[sourceIndexPath.section][sourceIndexPath.row]
        itemsList[sourceIndexPath.section].removeAtIndex(sourceIndexPath.row)
        itemsList[destinationIndexPath.section].insert(itemToMove, atIndex: destinationIndexPath.row)
    }


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
                  return CGSizeMake(collectionView.bounds.size.width, 50)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(collectionView.bounds.size.width, 40)
    }

    func handleGesture(gesture: UIPanGestureRecognizer) {
        switch(gesture.state) {
        case UIGestureRecognizerState.Began:

            guard let selectedIndexPath = self.collectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
                break }
            collectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
        case UIGestureRecognizerState.Changed:
            collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
        case UIGestureRecognizerState.Ended:
            collectionView.endInteractiveMovement()
        default:
            collectionView.cancelInteractiveMovement()
        }
    }
}


class CustomCell: UICollectionViewCell {
    var label: UILabel!
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        self.label = UILabel()
        self.label.translatesAutoresizingMaskIntoConstraints = false
        self.contentView.addSubview(self.label)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        let margins = self.contentView.layoutMarginsGuide
        self.label.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor).active = true
        self.label.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor).active = true
        self.label.centerYAnchor.constraintEqualToAnchor(self.contentView.centerYAnchor).active = true
    }
}



Best Answer-推荐答案


根据 Apple 的说法,我的问题中描述的问题是 iOS 中的一个错误,并且似乎在当前版本的 iOS 10 beta (xcode 8 GM) 中得到了解决。

不幸的是, Collection View 的其他问题尚未修复,例如将项目移动到空白部分的基本支持。

关于ios - 将 Collection View 中的单元格移动到另一部分的最后一个单元格之外不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39318624/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap