Я потратил дни на то, чтобы пробовать столько разных вещей, чтобы заставить эту работу работать, но постоянно терплю неудачу. Это начинает угнетать, и мне очень хотелось бы получить идею от кого-то, кто знает, как заставить пользователя перетаскивать и переупорядочивать элементы в NSCollectionView. Это моя последняя попытка, которая абсолютно ничего не делает:
let MyItemType = "myItemType"
class CollectionViewController: NSViewController {
@IBOutlet weak var collectionView: NSCollectionView!
let MyItemType = "myItemType"
override func viewDidLoad() {
super.viewDidLoad()
configureCollectionView()
loadDevices()
getstate()
// try to register drag and drop (all attempts fail)
//collectionView.register(forDraggedTypes: [MyItemType, NSFilenamesPboardType]
}
// more functions here were omitted...
}
extension CollectionViewController : NSCollectionViewDataSource {
func numberOfSections(in collectionView: NSCollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return groupbuttons.count
}
func collectionView(_ itemForRepresentedObjectAtcollectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: "CollectionViewItem", for: indexPath)
guard let collectionViewItem = item as? CollectionViewItem else {return item}
let button = groupbuttons[indexPath.item]
collectionViewItem.button = button
return item
}
/* try to implement drag and drop reordering (all attempts fail)
func collectionView(collectionView: NSCollectionView, canDragItemsAtIndexes indexes: NSIndexSet, withEvent event: NSEvent) -> Bool {
return true
}
func collectionView(_ collectionView: NSCollectionView, writeItemsAtIndexes indexes: NSIndexSet, to toPasteboard: NSPasteboard) -> Bool {
let data = NSKeyedArchiver.archivedData(withRootObject: [indexes])
toPasteboard.declareTypes([MyItemType], owner:self)
toPasteboard.setData(data, forType:MyItemType)
return true
}
func collectionView(_ collectionView: NSCollectionView, validateDrop info: NSDraggingInfo, proposedIndex index: Int, proposedDropOperation dropOperation: NSCollectionViewDropOperation) -> NSDragOperation {
//collectionView.setDropIndex(index, dropOperation: NSCollectionViewDropOperation.above)
return NSDragOperation.move
}
func collectionView(_ collectionView: NSCollectionView, acceptDrop info: NSDraggingInfo, index: Int, dropOperation: NSTableViewDropOperation) -> Bool {
let pasteboard = info.draggingPasteboard()
let itemData = pasteboard.data(forType: MyItemType)
if(itemData != nil) {
var dataArray = NSKeyedUnarchiver.unarchiveObject(with: itemData!) as! Array<IndexSet>,
indexSet = dataArray[0]
let movingFromIndex = indexSet.first
let item = groupbuttons[movingFromIndex!]
_moveItem(item, from: movingFromIndex!, to: index)
return true
}
else {
return false
}
}
func _moveItem(_ item: GroupButton, from: Int, to: Int) {
groupbuttons.remove(at: from)
if(to > groupbuttons.endIndex) {
groupbuttons.append(item)
}
else {
groupbuttons.insert(item, at: to)
}
collectionView.reloadData()
}
*/
}
Если кому-то удалось получить перетаскивание для работы с NSCollectionView, я был бы признателен за любую помощь. Или, может быть, в данном случае это невозможно? Я не уверен, почему переупорядочивание элементов перетаскиванием так сложно.