加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:56:26 所属栏目:百科 来源:网络整理
导读:我使用 swift 3,我有一个NSTableView(3列). 我填写如下数据: func tableView(_ tableView: NSTableView,viewFor tableColumn: NSTableColumn?,row: Int) - NSView? { var cellIdentifier = String() var cellText = String() switch tableColumn { case tab
我使用 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变量的值.

吉林大学.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读