对NSTableView列的文本字段的所有值求和

Gho*_*108 5 macos swift3 xcode8

我使用swift 3,我有一个NSTableView(3列).我填写如下数据:

 func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

        var cellIdentifier = String()
        var cellText = String()

            switch tableColumn {
                case tablewView.tableColumns[0]?:
                    cellText = "100"
                    cellIdentifier = "Cell1"
                break

                case tablewView.tableColumns[1]?:
                    cellText = "100"
                    cellIdentifier = "Cell2"
                break

                case tablewView.tableColumns[2]?:
                    cellText = "100"
                    cellIdentifier = "Cell3"
                break

                default: break
            }

            if let view = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
                view.textField?.stringValue = cellText
                return view
            }

        return nil
    }
Run Code Online (Sandbox Code Playgroud)

现在,我想在每次选择改变时对第1列的所有值求和.我怎么能意识到这一点?

Men*_*tes 1

要添加值,您必须保证所有值都是数字,或者至少可以转换为数字。

之后,需要维护一个变量来接收来自的值的增量tablewView.tableColumns[1]?

前任:

var sum = 0

func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {

    var cellIdentifier = String()
    var cellText = String()

        switch tableColumn {
            case tablewView.tableColumns[0]?:
                cellText = "100"
                cellIdentifier = "Cell1"
            break

            case tablewView.tableColumns[1]?:
                cellText = "100"
                sum = sum + Int(cellText)
                cellIdentifier = "Cell2"
            break

            case tablewView.tableColumns[2]?:
                cellText = "100"
                cellIdentifier = "Cell3"
            break

            default: break
        }

        if let view = tableView.make(withIdentifier: cellIdentifier, owner: nil) as? NSTableCellView {
            view.textField?.stringValue = cellText
            return view
        }

    return nil
}
Run Code Online (Sandbox Code Playgroud)

因此,viewWillLayout()您可以使用一些标签来显示变量的值sum

吉林大学。