【问题标题】:Drag and Dropping on a String/Images NSTableView Swift拖放字符串/图像 NSTableView Swift
【发布时间】:2016-08-09 02:04:19
【问题描述】:

我试图创建一个基于单元格表(索引从 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
}

【问题讨论】:

    标签: swift macos drag-and-drop nstableview


    【解决方案1】:

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

    tableView.registerForDraggedTypes([NSGeneralPboard])
    

    谢谢!

    【讨论】:

    • 你知道 Swift 4 上的等价物吗?
    猜你喜欢
    • 1970-01-01
    • 2010-10-13
    • 2012-05-05
    • 1970-01-01
    • 2011-08-22
    • 2015-09-02
    • 2016-09-03
    • 1970-01-01
    相关资源
    最近更新 更多