微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我使用 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
    }

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

解决方法

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

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

例如:

var sum = 0

func tableView(_ tableView: NSTableView,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变量的值.

吉林大学.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐