コレクションビューでセルを移動/パンできるようにするには、UIGestureRecognizerを実装する必要があります。これは、単一セクションのコレクションビューで正常に機能します。ただし、複数セクションのコレクションビューでは、セルが別のセクションの最後のセルを超えてパン/移動されるとエラーが発生します。「cellForItemAtIndexPath」は、パン中に「新しい」セルを表示するように正しくトリガーされますが、最後に使用可能なセルを超えてパンした後にトリガーしないでください(つまり、その状況ではindexPath.row == numberOfItemsInSectionであり、エラーが発生します(以下のコードを参照))。 。
テーブルビューでは、この問題は(デフォルトの)移動/パンの実装では発生しません。
これはSwiftのバグである可能性があり、セクションの最後のセルを超えてパンしているときに「cellForItemAtIndexPath」のトリガーを回避するための提案はありますか?
以下は、問題を説明するためのSwift2.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
}
}
Appleによると、私の質問で説明されている問題はiOSのバグであり、iOS 10ベータ版(xcode 8 GM)の現在のリリースで解決されたようです。
残念ながら、アイテムを空のセクションに移動するための基本的なサポートなど、コレクションビューに関するその他の問題はまだ修正されていません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加