gpt4 book ai didi

macos - NSTableview - 拖放分为两个类和 Controller

转载 作者:行者123 更新时间:2023-12-03 17:37:54 25 4
gpt4 key购买 nike

我发现这个教程对于使用 nstabelview 实现拖放很有帮助: https://drive.google.com/open?id=0B8PBtMQt9GdONzV3emZGQWUtdmM

这很好用。

但我想将两个 TableView 拆分为不同的 View Controller 和具有 Split View的类:

一个分割 View Controller :

  • 第 1 项:带有源 nstableview 的 View Controller (SourceTableView.class)
  • 第 2 项:带有目标 nstableview (TargetTableView.class) 的 View Controller

我该如何在这个项目中做到这一点?我知道如何在 Storyboard中创建分割 View Controller 。但我不知道,如果我有两个不同的类,类 SourceTableView.class 的 iBoutlet SourceTabelView 如何分配类 TargetTableView.class 的 iBoutlet TargetTableView

更新

var person = [Person]()

NSManagedObject.class

import Foundation
import CoreData

@objc(Person)
public class Person: NSManagedObject {
@NSManaged public var firstName: String
@NSManaged public var secondName: String
}

最佳答案

在 Split View内的两个 TableView 之间拖放的示例。在一个 TableView 内拖动并进行多项选择即可。按住 Option 键拖动副本。

每个 TableView 的数据源是分割 View 内的 View Controller 。每个 TableView 都有自己的 View Controller ,每个 View Controller 控制一个 TableView 。两个 View Controller 都是相同的 NSViewController 子类:

class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {

@IBOutlet weak var myTableView: NSTableView!
var dataArray: NSMutableArray = ["John Doe", "Jane Doe", "Mary Jane"]

override func viewDidLoad() {
super.viewDidLoad()
myTableView.register(forDraggedTypes: ["com.yoursite.yourproject.yourstringstype"])
}

override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}

// NSTableViewDataSource data methods

func numberOfRows(in tableView: NSTableView) -> Int {
return dataArray.count
}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return dataArray[row] as AnyObject!;
}

// NSTableViewDataSource drag methods

func tableView(_ tableView: NSTableView, writeRowsWith rowIndexes: IndexSet, to pboard: NSPasteboard) -> Bool {
// the dragging destination needs the strings of the rows to add to its own data,
// we, the dragging source, need the indexes of the rows to remove the dropped rows.
pboard.declareTypes(["com.yoursite.yourproject.yourstringstype", "com.yoursite.yourproject.yourindexestype"],
owner: nil)
pboard.setData(NSKeyedArchiver.archivedData(withRootObject: (dataArray as NSArray).objects(at:rowIndexes as IndexSet)), forType: "com.yoursite.yourproject.yourstringstype")
pboard.setData(NSKeyedArchiver.archivedData(withRootObject: rowIndexes), forType: "com.yoursite.yourproject.yourindexestype")
return true
}

func tableView(_ tableView: NSTableView, draggingSession session: NSDraggingSession,
endedAt screenPoint: NSPoint, operation: NSDragOperation) {
// remove the dragged rows if the rows are dragged to the trash or were moved to somewhere else.
var removeRows = false
if operation == .delete {
// trash
removeRows = true
} else if operation == .move {
// check if the point where the rows were dropped is inside our table view.
let windowRect = tableView.convert(tableView.bounds, to: nil)
let screenRect = view.window!.convertToScreen(windowRect)
if !NSPointInRect(screenPoint, screenRect) {
removeRows = true
}
}
if removeRows {
// remove the rows, the indexes are on the pasteboard
let data = session.draggingPasteboard.data(forType: "com.yoursite.yourproject.yourindexestype")!
let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSIndexSet
(dataArray as NSMutableArray).removeObjects(at: rowIndexes as IndexSet)
tableView.reloadData()
}
}

// NSTableViewDataSource drop methods

func tableView(_ tableView: NSTableView, validateDrop info: NSDraggingInfo, proposedRow row: Int,
proposedDropOperation dropOperation: NSTableViewDropOperation) -> NSDragOperation {
// only accept drop above rows, not on rows.
if dropOperation == .above {
// return move if the dragging source allows move
if info.draggingSourceOperationMask().contains(.move) {
return .move
}
// return copy if the dragging source allows copy
if info.draggingSourceOperationMask().contains(.copy) {
return .copy
}
}
return []
}

func tableView(_ tableView: NSTableView, acceptDrop info: NSDraggingInfo, row: Int,
dropOperation: NSTableViewDropOperation) -> Bool {
// if the rows were moved inside the same table view we do a reorder
var dropRow = row
if info.draggingSource() as AnyObject === myTableView as AnyObject &&
info.draggingSourceOperationMask().contains(.move) {
// remove the rows from their old position
let data = info.draggingPasteboard().data(forType: "com.yoursite.yourproject.yourindexestype")!
let rowIndexes = NSKeyedUnarchiver.unarchiveObject(with: data) as! NSIndexSet
(dataArray as NSMutableArray).removeObjects(at: rowIndexes as IndexSet)
// recalculate the row of the drop
dropRow -= rowIndexes.countOfIndexes(in: NSMakeRange(0, dropRow))
}
// insert the dragged rows
let data = info.draggingPasteboard().data(forType: "com.yoursite.yourproject.yourstringstype")!
let draggedStrings = NSKeyedUnarchiver.unarchiveObject(with: data) as! [Any]
dataArray.insert(draggedStrings, at:IndexSet(integersIn:dropRow..<(dropRow + draggedStrings.count)))
tableView.reloadData()
return true
}

}

要使拖动到垃圾箱工作,子类化 NSTableView 并覆盖:

override func draggingSession(_ session: NSDraggingSession, sourceOperationMaskFor
context: NSDraggingContext) -> NSDragOperation {
let test = super.draggingSession(session, sourceOperationMaskFor: context)
Swift.print("sourceOperationMaskFor \(test)")
switch context {
case .withinApplication:
return [.move, .copy]
case .outsideApplication:
return [.delete]
}
}

附:我不熟悉 Swift,并且在数组和索引集方面遇到一些问题,因此我使用了 NSMutableArrayNSIndexSet

关于macos - NSTableview - 拖放分为两个类和 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com