使用拖放重新排序nstableview中的行

时间:2017-06-06 13:09:27

标签: macos cocoa swift3 xcode8 nstableview

我有这样的4行的nstableview:

item 1
item 2
item 3
item 4

现在我想在项目3和4之间拖动项目1,如下所示:

item 2
item 3
item 1 < position after drag and drop
item 4

这是我的代码:

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


   let rowData = info.draggingPasteboard().data(forType: NSStringPboardType)
   let dataArray = NSKeyedUnarchiver.unarchiveObject(with: rowData!) as! Array<IndexSet>
   let draggedItem = rightDataArray[dataArray[0].first!]
   rightDataArray.remove(at: dataArray[0].first!)

   print("Target Row for draggedItem: ",row)

   if row > rightDataArray.endIndex {
      rightDataArray.append(draggedItem)
   } else {
      rightDataArray.insert(draggedItem, at: row)
   }

   self.tableView.reloadData()

   return true

}

问题是我的结果是这样的:

item 2
item 3
item 4
item 1 < wrong position after drag and drop

打印结果:

  

draggedItem的目标行:3

任何人都有想法?

0 个答案:

没有答案