2016-08-09 9 views
0

ドラッグに応じて変更されるNSImage配列に接続するセルのテーブル(1から17のインデックス)に基づいてドラッグアンドドロップシステムを作成しようとしています。結果をドロップします。私のテーブルはドラッグを受け入れるようです(少し透明な数字は移動可能です)、ドロップすると何も起こりません。私はタイプ登録に何か問題があると思うが、私はosxでちょっと新しくなっている。助けてもらえますか?文字列/画像のドラッグ&ドロップNSTableView Swift

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 
} 

答えて

0

深い検索の後、私は自分のタイプを登録していないことがわかりました! Newby間違い..

tableView.registerForDraggedTypes([NSGeneralPboard]) 

ありがとう!

+0

スウィフト4でこれに相当することは分かっていますか? –

関連する問題