【问题标题】:Iterating through every tableView cells with data source使用数据源遍历每个 tableView 单元格
【发布时间】:2018-08-15 09:01:05
【问题描述】:

我正在尝试通过tableView 中的每个cell iterate 使用amountValue 值进行计算。

我写了一个工作方法迭代tableView而不是data source,你们有些不高兴哈哈。

如何将其转换为 data source 迭代?

    // CryptosMO is the Managed Object with an `NSNumber` `amountValue` attribute among others.

    var item : [CryptosMO] = []

    if CDHandler.fetchObject() != nil {
        item = CDHandler.fetchObject()
    }

    for section in 0...self.tableView.numberOfSections {

        if (self.tableView.numberOfRows(inSection: section) > 1) {
            for row in 0...self.tableView.numberOfRows(inSection: section) {
                let indexPath: IndexPath = IndexPath(row: row, section: section)

                if item[indexPath.row].amountValue != nil {
                    let total = item[indexPath.row].amountValue.doubleValue + item[indexPath.row + 1].amountValue.doubleValue
                    print("TOTAL :", total)

                }

            }
        }
    }

【问题讨论】:

  • 为什么要迭代 tableview 来获取总数?不是数据源?
  • @mnemonic23 感谢您的回答,这正是我想要做的。你能告诉我怎么做吗?

标签: swift function uitableview core-data datasource


【解决方案1】:

在没有 for 循环的情况下计算总数。

let total = dataSource.reduce(0.0, { $0 + ($1.amountValue.doubleValue) } )

也许您的数据源是“项目”?

【讨论】:

  • 这正是我想要的,非常感谢!我需要阅读更多关于 data sourcemap / reduce 的信息,这看起来很强大!
猜你喜欢
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 2015-01-05
  • 2018-08-26
  • 1970-01-01
  • 2019-12-04
相关资源
最近更新 更多