hello @gmm,
I am a self-taught novice programmer that has done most of my work in Objective C. I wanted to convert some apps I developed to Swift, one which has a CollectionView. My research on doing this brought me to your excellent CollectionView tutorials: Advanced Collection Views in OS X Tutorial and NSCollectionView Tutorial. Like gili I too am able to drag but when dropping nothing happens. Also, I never get the blue line denoting where the drop should take place. I will post the delegate below. From best I can tell the delegate methods for dropping are never called (I setup print statements at the beginning of each method). The results are:
TRACE->ViewController ->NSCollectionViewDelegate-01 in method collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set)
TRACE->ViewController in method highlightItems(_ selected: Bool, atIndexPaths: Set)
<ADAP.CollectionViewItem: 0x610000141d90>{represented object: (null), view: <NSView: 0x610000121fe0> (frame {{267.5, 30}, {160, 140}}), selected: YES}
TRACE->ViewController ->NSCollectionViewDelegate-06 in method collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) → NSPasteboardWriting?
image file to pasteboard
TRACE->ImageDirectoryLoader: NSObject in method imageFileForIndexPath(_ indexPath: IndexPath) → ImageFile
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->StickyHeadersLayout: NSCollectionViewFlowLayout in method override func layoutAttributesForElements(in rect: NSRect) → [NSCollectionViewLayoutAttributes]
TRACE->ViewController ->NSCollectionViewDelegate-08 in method collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation)
The version of software I’m using:
XCODE: Version 8.3.2 (8E2002)
MACOS Development Target 10.12
Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)
I believe my lack of knowledge when converting Swift Structures to Swift 3 are to blame.
This is my first post so if I’m doing something inappropriate let me know. Any help you can provide would be appreciated.
extension ViewController : NSCollectionViewDelegate {
func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
let vExtension = "NSCollectionViewDelegate-01"
let method = "collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
highlightItems(true, atIndexPaths: indexPaths)
}
func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
let vExtension = "NSCollectionViewDelegate-02"
let method = "collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
highlightItems(false, atIndexPaths: indexPaths)
}
// 1
private func collectionView(collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAtPoint screenPoint: NSPoint, forItemsAtIndexPaths indexPaths: Set<NSIndexPath>) {
let vExtension = "NSCollectionViewDelegate-03"
let method = "func collectionView(collectionView: NSCollectionView, draggingSession session: NSDraggingSession, willBeginAtPoint screenPoint: NSPoint, forItemsAtIndexPaths indexPaths: Set<NSIndexPath>) "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
indexPathsOfItemsBeingDragged = indexPaths
}
// 2
@nonobjc func collectionView(collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath?>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation {
let vExtension = "NSCollectionViewDelegate-04"
let method = "func collectionView(collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo, proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath?>, dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionViewDropOperation>) -> NSDragOperation "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
// 3
if proposedDropOperation.pointee == NSCollectionViewDropOperation.on {
proposedDropOperation.pointee = NSCollectionViewDropOperation.before
}
// 4
if indexPathsOfItemsBeingDragged == nil {
return NSDragOperation.copy
} else {
return NSDragOperation.move
}
}
// 1
@nonobjc func collectionView(collectionView: NSCollectionView, canDragItemsAtIndexes indexes: NSIndexSet, withEvent event: NSEvent) -> Bool {
let vExtension = "NSCollectionViewDelegate-05"
let method = "collectionView(collectionView: NSCollectionView, canDragItemsAtIndexes indexes: NSIndexSet, withEvent event: NSEvent) -> Bool "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
print("returning true from drag")
return true
}
// 2
func collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
let vExtension = "NSCollectionViewDelegate-06"
let method = "collectionView(_ collectionView: NSCollectionView, pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
print("image file to pasteboard")
let imageFile = imageDirectoryLoader.imageFileForIndexPath(indexPath as IndexPath)
return imageFile.url?.absoluteURL as NSPasteboardWriting?
}
// 1
func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionViewDropOperation) -> Bool {
let vExtension = "NSCollectionViewDelegate-07"
let method = "collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo, indexPath: IndexPath, dropOperation: NSCollectionViewDropOperation) -> Bool "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
if indexPathsOfItemsBeingDragged != nil {
// 2
let indexPathOfFirstItemBeingDragged = indexPathsOfItemsBeingDragged.first!
var toIndexPath: NSIndexPath
if indexPathOfFirstItemBeingDragged.compare(indexPath as IndexPath) == .orderedAscending {
toIndexPath = NSIndexPath(forItem: indexPath.item-1, inSection: indexPath.section)
} else {
toIndexPath = NSIndexPath(forItem: indexPath.item, inSection: indexPath.section)
}
// 3
imageDirectoryLoader.moveImageFromIndexPath(indexPath: indexPathOfFirstItemBeingDragged, toIndexPath: toIndexPath)
// 4
collectionView.moveItem(at: indexPathOfFirstItemBeingDragged as IndexPath, to: toIndexPath as IndexPath)
} else {
// 5
var droppedObjects = Array<NSURL>()
draggingInfo.enumerateDraggingItems(options: NSDraggingItemEnumerationOptions.concurrent, for: collectionView, classes: [NSURL.self], searchOptions: [NSPasteboardURLReadingFileURLsOnlyKey : NSNumber(value: true)]) { (draggingItem, idx, stop) in
if let url = draggingItem.item as? NSURL {
droppedObjects.append(url)
}
}
// 6
insertAtIndexPathFromURLs(urls: droppedObjects, atIndexPath: indexPath as NSIndexPath)
}
return true
}
// 7
func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
let vExtension = "NSCollectionViewDelegate-08"
let method = "collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession, endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) "
print("TRACE->\(module) ->\(vExtension) in method \(method)")
indexPathsOfItemsBeingDragged = nil
}