UICollectionViewDragDelegate isn't working

1.2k views Asked by At

I'm successfully implementing a UICollectionView that display some dummy texts on screen. Everything is working fine and now I added the drag feature implementing self.collectionView.dragDelegate = self and the UICollectionViewDelegateFlowLayout, but it doesn't work. Am I missing something more?

final class XptoComponent: UIView {

    private var titles = ["Xpto 1", "Xpto 2", "Xpto 3", "Xpto 4", "Xpto 5", "Xpto 6", "Xpto 7", "Xpto 8", "Xpto 9"]

    private var collectionView: UICollectionView = {

        let collectionViewLayout = UICollectionViewFlowLayout()
        collectionViewLayout.minimumInteritemSpacing = 0
        collectionViewLayout.minimumLineSpacing = 0
        collectionViewLayout.itemSize = CGSize(width: UIScreen.main.bounds.size.width, height: NumericConstant.ViewCell.height)
        collectionViewLayout.sectionInset = .zero

        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout).usingAutoLayout()
        collectionView.registerCell(XptoCollectionViewCell.self)

        return collectionView
    }()

    init() {

        super.init(frame: .zero)

        self.collectionView.dataSource = self
        self.collectionView.delegate = self
        self.collectionView.dragDelegate = self

        self.defineSubviews()
        self.defineSubviewsConstraints()
    }

    private func defineSubviewsConstraints() {

        NSLayoutConstraint.activate([

            self.collectionView.topAnchor.constraint(equalTo: self.topAnchor),
            self.collectionView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
            self.collectionView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
            self.collectionView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor)
        ])
    }

    private func defineSubviews() {

        self.addSubview(self.collectionView)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension XptoComponent: UICollectionViewDataSource {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return self.titles.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(XptoCollectionViewCell.self, for: indexPath)
        cell?.titleLabel.text = self.titles[indexPath.row]

        return cell ?? UICollectionViewCell()
    }
}

extension XptoComponent: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        return CGSize(width: self.frame.width, height: 100)
    }
}

extension XptoComponent: UICollectionViewDragDelegate {

    func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {

        let title = self.titles[indexPath.item]
        let itemProvider = NSItemProvider(object: title as NSString)
        let dragItem = UIDragItem(itemProvider: itemProvider)

        return [dragItem]
    }
}
2

There are 2 answers

3
Jawad Ali On BEST ANSWER

You need to set this variable dragInteractionEnabled to true

 collectionView.dragInteractionEnabled = true
0
Tamás Sengel On

Make sure to return something in the collectionView(_:itemsForBeginning:at:) function. Returning an empty array works for a UITableView but not for a UICollectionView.

Also, check out what you return from collectionView(_:canHandle:). If you return false, that prevents the drag from happening.