通过滑动从 NSTableview 中删除行

Remove row from NSTableview by swiping

我正在尝试通过滑动功能从我的 NStableView 中删除一行。我使用的是 macOS 10.13 和 swift 4,并且是 swift 编码的新手。

我尝试遵循: 但它对我不起作用。

这就是我实现它的方式:

public func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
    // left swipe
    if edge == .trailing {
        let deleteAction = NSTableViewRowAction(style: .destructive, title: "Delete", handler: { (rowAction, row) in
            // action code
            List_Symbol.remove(at: row)
            List_Price.remove(at: row)
            List_Procent.remove(at: row)
            List_Volume.remove(at: row)
        })
        deleteAction.backgroundColor = NSColor.red
        return [deleteAction]
    }
    let archiveAction = NSTableViewRowAction(style: .regular, title: "Archive", handler: { (rowAction, row) in
        // action code
    })
    return [archiveAction]
}

关于如何通过滑动删除 NSTableView 中的行还有其他建议吗?

完整代码:

import Cocoa
import Alamofire

var List_Symbol = ["AAPL"]
var List_Price  = [10.0]
var List_Procent = [1.0]
var List_Volume = [100]


class ViewController: NSViewController, NSTableViewDataSource,     NSTableViewDelegate {
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var input: NSTextField!
@IBAction func additem(_ sender: Any) {
    if (input.stringValue != ""){
        var Stock = StockInformation(symbol: input.stringValue)
        Stock.GetStockInformation {
            List_Symbol.append(Stock.Symbol)
            List_Price.append(Stock.Price)
            List_Procent.append(Stock.Percent)
            List_Volume.append(Stock.Volume)
            self.tableView.reloadData()
        }
        input.stringValue = ""
    }
    self.tableView.reloadData()
}

public func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
    // left swipe
    if edge == .trailing {
        let deleteAction = NSTableViewRowAction(style: .destructive, title: "Delete", handler: { (rowAction, row) in
            // action code
            List_Symbol.remove(at: row)
            List_Price.remove(at: row)
            List_Procent.remove(at: row)
            List_Volume.remove(at: row)
            tableView.removeRows(at: IndexSet(integer: row), withAnimation: .effectFade)
        })
        deleteAction.backgroundColor = NSColor.red
        return [deleteAction]
    }
    let archiveAction = NSTableViewRowAction(style: .regular, title: "Archive", handler: { (rowAction, row) in
        // action code
    })
    return [archiveAction]
}



override func viewDidAppear() {
    tableView.reloadData()
}


override func viewDidLoad() {
    super.viewDidLoad()

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

 func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any?{
    var identifierStr = tableColumn!.identifier
    if (identifierStr.rawValue == "StockNameCellID"){
        return List_Symbol[row]
    }else if (identifierStr.rawValue == "PriceCellID"){
        return List_Price[row]
    }else if (identifierStr.rawValue == "PercentCellID"){
        return List_Procent[row]
    }else if (identifierStr.rawValue == "VolumeCellID"){
        return List_Volume[row]
    }
    tableView.reloadData()
    return nil
}

}

不要使用多个数组作为数据源,那太可怕了。

在 macOS 中,您可以使用 Cocoa 绑定

替换大量样板代码

• 使用从 NSObject 继承的 class 作为数据源

@objcMembers
class Stock : NSObject {
    dynamic var symbol : String
    dynamic var price : Double
    dynamic var procent : Double
    dynamic var volume : Int

    init(symbol : String, price : Double, procent : Double, volume : Int) {
        self.symbol = symbol
        self.price = price
        self.procent = procent
        self.volume = volume
    }
}

• 在视图控制器中声明数据源数组

@objc var stocks = [Stock]()

• 在 additem 中创建一个新的 Stock 项,而不是重新加载 table 视图仅插入带有智能动画的行

@IBAction func additem(_ sender: Any) {
    if !input.stringValue.isEmpty {
        let stock = StockInformation(symbol: input.stringValue)
        stock.GetStockInformation {
            let newStock = Stock(symbol: stock.Symbol, price: stock.Price, procent: stock.Percent, volume: stock.Volume)
            let insertionIndex = IndexSet(integer: stocks.count)
            self.stocks.append(newStock)
            self.tableView.insertRows(at: insertionIndex, withAnimation: .effectGap)
            self.input.stringValue = ""
        }
    }
}

objectValueFornumberOfRows分别只有一行

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

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
    return stocks[row]
}

delete 操作是

public func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
    print("swipe")
    // left swipe
    if edge == .trailing {
        let deleteAction = NSTableViewRowAction(style: .destructive, title: "Delete", handler: { (rowAction, row) in
            // action code
            self.stocks.remove(at: row)
            tableView.removeRows(at: IndexSet(integer: row), withAnimation: .effectFade)
        })
        deleteAction.backgroundColor = NSColor.red
        return [deleteAction]
    }
}

• 在Interface Builder 中,将table 视图的数据源和委托连接到视图控制器。进一步按 ⌥⌘7(绑定检查器),然后 select 每个 Table View CellNSTextField 不是 NSTextFieldCell and not Table Cell View!) 并将 Value 绑定到 Table Cell View 并将 Model Key Path 绑定到相应的 属性(objectValue.symbolobjectValue.price等)

您甚至可以通过将 table 视图的 content 绑定到 stocks 数组来使用更多 Cocoa Bindings。然后你可以摆脱数据源及其方法。