拖放字符串/图像NSTableView Swift

时间:2016-08-09 02:04:20

标签: swift macos drag-and-drop nstableview

我试图创建一个基于单元格表(索引从1到17)的拖放系统,该表连接到NSImage数组,根据拖放结果进行修改。我的表似乎接受拖动(带有一点透明度的数字是可移动的),但是当我放下时没有任何反应。我怀疑我的类型注册有问题,但我在osx有点新。有人可以帮忙吗?

var images:[NSImage] = []

func getImages() {
    images = []
    for i in 50...66 {
        images.append(NSImage(named: "IMG_67\(i)")!)
    }
}

func tableViewSelectionDidChange(notification: NSNotification) {
    let table = notification.object
    let selection = table?.selectedRow
    imgView.image = images[selection!]
}

func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
    tableView.cell?.title = String(row)

    return row + 1
}

func numberOfRowsInTableView(tableView: NSTableView) -> Int {

    getImages()

    return images.count
}

func tableView(tableView: NSTableView, writeRowsWithIndexes rowIndexes: NSIndexSet, toPasteboard pboard: NSPasteboard) -> Bool {

    let data:NSData = NSKeyedArchiver.archivedDataWithRootObject(rowIndexes)
    let registeredTypes:[String] = [NSGeneralPboard] //Problem might be here
    pboard.declareTypes(registeredTypes, owner: self)
    pboard.setData(data, forType: NSGeneralPboard)

    return true
}

func tableView(tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int, proposedDropOperation dropOperation: NSTableViewDropOperation) -> NSDragOperation {

    if dropOperation == .Above {
        return .Move
    }
    return .All
}

func tableView(tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int, dropOperation: NSTableViewDropOperation) -> Bool {

    let data: NSData = info.draggingPasteboard().dataForType(NSGeneralPboard)!
    let rowIndexes: NSIndexSet = NSKeyedUnarchiver.unarchiveObjectWithData(data) as! NSIndexSet
    let value: NSImage = images[rowIndexes.firstIndex]
    images.removeAtIndex(rowIndexes.firstIndex)
    if (row > images.count)
    {
        images.insert(value, atIndex: row - 1)
    } else {
        images.insert(value, atIndex: row)
    }
    tableView.reloadData()
    print("returning true") //Not getting called at all
    return true
}

1 个答案:

答案 0 :(得分:0)

经过深入搜索,我发现我没有注册我的类型!纽比错误..

tableView.registerForDraggedTypes([NSGeneralPboard])

谢谢!