У меня есть UICollectionView, который загружается в UIViewController. Я хочу активировать перемещение по клеткам в представлении коллекции, но у меня возникли проблемы. Я пытаюсь использовать простой подход IOS9 moveItemAt следующим образом:
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath:
IndexPath, to destinationIndexPath: IndexPath) {
let temp = cardData.remove(at: sourceIndexPath.item)
cardData.insert(temp, at: destinationIndexPath.item)
print(cardData)
}
Но это не работает. Длинные нажатия не распознаются вообще. Кто-нибудь знает, как заставить это работать, используя программно сгенерированный UICollectionView, который загружен в UIViewController? Спасибо.
Просмотр контроллера:
import UIKit
import SDWebImage
class EditDeckController: UIViewController {
var deckData: DisplayDeck!
var cardData = [String]()
fileprivate let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.translatesAutoresizingMaskIntoConstraints = false
cv.register(EditDeckDataCell.self, forCellWithReuseIdentifier: "Cell")
return cv
}()
override func viewDidLoad() {
super.viewDidLoad()
title = K.appName
view.backgroundColor = .black
view.addSubview(collectionView)
collectionView.backgroundColor = .blue
collectionView.delegate = self
collectionView.dataSource = self
collectionView.topAnchor.constraint(equalTo: view.topAnchor,constant: 40).isActive =
true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 40).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: -40).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor,constant: -40).isActive = true
cardData = deckData.deckData.components(separatedBy: ",")
print(cardData)
}
}
extension EditDeckController: UICollectionViewDelegateFlowLayout, UICollectionViewDelegate,
UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout:
UICollectionViewLayout, sizeForItemAt indexpath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width/2.1, height:
(collectionView.frame.width/2.1)*1.3953)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int)
-> Int {
return cardData.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! EditDeckDataCell
cell.backgroundColor = .red
cell.bg.sd_setImage(with: URL(string: "\(K.liveAssetEnpoint)/studio/member/\(deckData.teamMemberId)/thm/\(cardData[indexPath.row]).png"))
return cell
}
func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let temp = cardData.remove(at: sourceIndexPath.item)
cardData.insert(temp, at: destinationIndexPath.item)
print(cardData)
}
}
Класс ячейки:
import UIKit
class EditDeckDataCell: UICollectionViewCell {
let bg: UIImageView = {
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.contentMode = .scaleAspectFill
iv.clipsToBounds = true
iv.image = UIImage(named: "white-logo")
iv.layer.cornerRadius = 12
return iv
}()
override init(frame: CGRect) {
super.init(frame: .zero)
contentView.addSubview(bg)
bg.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
bg.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
bg.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
bg.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}