【发布时间】:2018-11-09 15:33:46
【问题描述】:
如何计算不同单元格中相同列的总和,而不是同一单元格中的总和。
不知道怎么解决。
import UIKit
class ResultViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {
@IBOutlet var tableview: UITableView!
@IBAction func backPage(_ sender: Any) {
self.presentingViewController?.dismiss(animated: true)
}
let ad2 = UIApplication.shared.delegate as? AppDelegate
@IBAction func resetHist(_ sender: Any) {
ad2?.listPrice = [String]()
ad2?.listAmt = [String]()
tableview.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ad2?.listPrice.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CustomTableViewCell
cell.resultPrice?.text = ad2?.listPrice[indexPath.row]
cell.resultAmt?.text = ad2?.listAmt[indexPath.row]
var sum = 0
// how to calculate sum of the same columns in different cells
for i in ad2?.listPrice.count {
sum += Int(ad2?.listPrice[i])
}
return cell
}
}
【问题讨论】:
-
你能举个例子吗?不太明白这里的列是什么意思。 (不确定您是否理解列的含义,无意冒犯)
-
对不起。我英语不成熟。 column 表示 tableviewcell 的每个元素 ex) listPrice[0], listPrice[1], ...
-
你打算在
cellForRowAt中使用它做什么?
标签: ios swift uitableview indexpath