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

时间:2017-05-24 12:56:23

标签: macos swift3 xcode8

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

gm convert -font helvetica -fill blue -pointsize 36 -draw "text 15,50 'hello'" source.jpg destination.jpg

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

1 个答案:

答案 0 :(得分: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
}

因此,在viewWillLayout(),您可以使用某个标签显示sum变量的值。

JLU。

相关问题