2017-09-27 4 views
0

NSCollectionViewの中の項目を並べ替えようとしていますが、機能しません。いくつかのデリゲートメソッドをvalidate dropaccept dropと呼びません。 func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting?またはfunc collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Boolが呼び出されますが、その後は他のメソッドは呼び出されません。Swift OSX - ドラッグ&ドロップでNSCollectionViewを再整理する

私はボットのドラッグアンドドロップのための正しいタイプを登録することができると思うので、コレクションビュー内のアイテムを移動すると、アイテムをドロップできる場所が表示されない私はそれを元の場所に戻ってバウンスそれをドロップします。

FotoProdutoLojaCollectionViewItem.swiftインポートココア

class FotoProdutoLojaCollectionViewItem: NSCollectionViewItem { 

    @IBOutlet weak var fotoProdutoLojaImageView: NSImageView! 
    @IBOutlet weak var fotoCapaImageView: NSImageView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     fotoCapaImageView.isHidden = true 
    } 

} 

ここCollectionView

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { 

     var item = NSCollectionViewItem() 

     item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FotoProdutoLojaCollectionViewItem"), for: indexPath) 

     let fotosProdutoLojaCollectionViewItem = item as! FotoProdutoLojaCollectionViewItem 

     produtoLoja?.fotos[indexPath.item].foto?.getDataInBackground(block: { 
      (data: Data?, error: Error?) -> Void in 

      if error == nil { 
       fotosProdutoLojaCollectionViewItem.fotoProdutoLojaImageView.image = NSImage(data: data!) 
      } 
     }) 

     if produtoLoja!.fotos[indexPath.item].imagemCapa { 
      fotosProdutoLojaCollectionViewItem.fotoCapaImageView.isHidden = false 
     }else { 
      fotosProdutoLojaCollectionViewItem.fotoCapaImageView.isHidden = true 
     } 

     return item 
    } 


override func viewDidLoad() { 
     super.viewDidLoad() 

     fotosProdutoLojaCollectionView.delegate = self 
     fotosProdutoLojaCollectionView.dataSource = self 
     fotosProdutoLojaCollectionView.registerForDraggedTypes([NSPasteboard.PasteboardType(kUTTypeData as String)]) 

     fotosProdutoLojaCollectionView.setDraggingSourceOperationMask(.move, forLocal: true) 
    } 

の商品pasteboardWriterForItemAt indexPathある:ここ

コードです。私はすべてのコメント行を試した。

func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? { 

    let pb = NSPasteboardItem() 

    var data: Data? 
    do { 
     try data = produtoLoja?.fotos[indexPath.item].foto?.getData() 
    } catch { 

    } 
    pb.setData(data!, forType: NSPasteboard.PasteboardType.string) 

    return pb 

    //return NSPasteboardItem() 
    //return data as? NSPasteboardWriting 
} 

ここにはwriteItemsAt indexPathsがあります。

func collectionView(_ collectionView: NSCollectionView, writeItemsAt indexPaths: Set<IndexPath>, to pasteboard: NSPasteboard) -> Bool { 

    return true 
} 

答えて

0

多くの方法が間違っていました。ここでは修正されたコード(アイテムをドラッグアンドドロップするコレクションビューのデリゲートとデータソースに関連する部分だけが)ある:

のviewDidLoad

var indiceItensMovidosDrag: Set<IndexPath> = [] 

override func viewDidLoad() { 
     super.viewDidLoad() 

     fotosProdutoLojaCollectionView.delegate = self 
     fotosProdutoLojaCollectionView.dataSource = self 
     fotosProdutoLojaCollectionView.registerForDraggedTypes([NSPasteboard.PasteboardType(kUTTypeItem as String)]) 

     fotosProdutoLojaCollectionView.setDraggingSourceOperationMask(.move, forLocal: true) 

     if produtoLoja == nil { 
      produtoLoja = ProdutoLoja() 
     } 
} 

デリゲートとデータソースメソッドのメソッド

func numberOfSections(in collectionView: NSCollectionView) -> Int { 

     return 1 
    } 

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

     return produtoLoja!.fotos.count 
    } 

    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { 

     var item = NSCollectionViewItem() 

     item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "FotoProdutoLojaCollectionViewItem"), for: indexPath) 

     let fotosProdutoLojaCollectionViewItem = item as! FotoProdutoLojaCollectionViewItem 

     produtoLoja?.fotos[indexPath.item].foto?.getDataInBackground(block: { 
      (data: Data?, error: Error?) -> Void in 

      if error == nil { 
       fotosProdutoLojaCollectionViewItem.fotoProdutoLojaImageView.image = NSImage(data: data!) 
      } 
     }) 

     if produtoLoja!.fotos[indexPath.item].imagemCapa { 
      fotosProdutoLojaCollectionViewItem.fotoCapaImageView.isHidden = false 
     }else { 
      fotosProdutoLojaCollectionViewItem.fotoCapaImageView.isHidden = true 
     } 

     return item 
    } 

func collectionView(_ collectionView: NSCollectionView, canDragItemsAt indexPaths: Set<IndexPath>, with event: NSEvent) -> Bool { 

     return true 
    } 

    func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? { 

     let retorno = NSPasteboardItem() 

     var data: Data? 
     do { 
      try data = produtoLoja?.fotos[indexPath.item].foto?.getData() 
     } catch { 

     } 

     retorno.setData(data!, forType: NSPasteboard.PasteboardType(kUTTypeItem as String)) 

     return retorno 
    } 

    func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) { 

     indiceItensMovidosDrag = indexPaths 
    } 

    func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) { 

     indiceItensMovidosDrag = [] 
    } 

    func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation { 

     if proposedDropOperation.pointee == NSCollectionView.DropOperation.on { 
      proposedDropOperation.pointee = NSCollectionView.DropOperation.before 
     } 

     return NSDragOperation.move 
    } 

    func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool { 

     var retorno = true 

     if indiceItensMovidosDrag.count == 1 { 
      for indice in indiceItensMovidosDrag { 
       collectionView.animator().moveItem(at: indice, to: (indexPath.item <= indice.item) ? indexPath : (IndexPath(item: indexPath.item - 1, section: 0))) 

      } 
     } else { 
      mostrarErro(mensagem: "Erro", informativo: "Só é possível mover uma imagem por vez") 
      retorno = false 
     } 

     //fotosProdutoLojaCollectionView.reloadData() 

     return retorno 
    } 
関連する問題