前提・実現したいこと
テーブルビューのセルを選択して、セクションごとの合計金額を次の画面で表示させたい。
現在、カテゴリー別に商品の合計金額を計算するアプリを作っています。商品を画面に表示させる事と、セルを複数選択することはできたのですが、セルを選択したときに値段を取得する方法が全くわかりませんでした。わかりやすく説明していただけるとありがたいです。お願いいたします。
発生している問題・エラーメッセージ
https://teratail.com/questions/209972?whotofollow=
こちらのページのベストアンサーの方の回答を参考に実装してみたのですが、うまく値段の値を取得することができません。
printコマンドで変数の値を表示できません。
private func calculate() { // セル選択時と選択解除時に呼ぶ indexPaths = tableView.indexPathsForSelectedRows! indexPaths.forEach { indexPath in // indexPath.section, indexPath.row から twoDimArray の対応する物を探す let name = twoDimArray[indexPath.section][indexPath.row] var price: Int if indexPath.section == 0 { // 対応から値段取得 Book の section だった場合 price = Book[name]! print(price) ←セルをタップしてもコンソールに何も表示されない。 } else if indexPath.section == 1 { //... } // 合計値を出したければ、セクションごとの合計用の変数を用意しておいて足していく } }
エラーメッセージ
該当のソースコード
import UIKit class TableViewController: UITableViewController { var mySections = [String]() var twoDimArray = [[String]]() var indexPaths = [IndexPath]() let Book = [ "BookA":1450, "BookB":550, "BookC":1100, "BookD":1000, "BookE":1250, "BookF":3000, "BookG":2500, "BookH":800, "BookI":850, "BookJ":2000 ] let CD = [ "CDA":1000, "CDB":1000, "CDC":2000, "CDD":1000, "CDE":1000, "CDF":1000, "CDG":3000, "CDH":2000, "CDI":2000, "CDJ":1000 ] let Food = [ "FoodA":200, "FoodB":100, "FoodC":220, "FoodD":120, "FoodE":125, "FoodF":130, "FoodG":300, "FoodH":285, "FoodI":20, "FoodJ":100 ] let Sport = [ "SportA":2000, "SportB":10000, "SportC":22000, "SportD":1200, "SportE":1250, "SportF":1300, "SportG":3000, "SportH":2850, "SportI":20000, "SportJ":1000 ] let Car = [ "CarA":2000, "CarB":1000, ] override func viewDidLoad() { super.viewDidLoad() mySections = ["本","CD","食品","スポーツ用品","カー用品"] for _ in 0 ... 4{ twoDimArray.append([]) } twoDimArray[0] = Array(Book.keys).sorted() twoDimArray[1] = Array(CD.keys).sorted() twoDimArray[2] = Array(Food.keys).sorted() twoDimArray[3] = Array(Sport.keys).sorted() twoDimArray[4] = Array(Car.keys).sorted() tableView.allowsMultipleSelection = true } override func numberOfSections(in tableView: UITableView) -> Int { return mySections.count } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return twoDimArray[section].count } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { return mySections[section] } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "NameCell", for: indexPath as IndexPath) cell.textLabel?.text = twoDimArray[indexPath.section][indexPath.row] return cell } private func calculate() { // セル選択時と選択解除時に呼ぶ indexPaths = tableView.indexPathsForSelectedRows! indexPaths.forEach { indexPath in // indexPath.section, indexPath.row から twoDimArray の対応する物を探す let name = twoDimArray[indexPath.section][indexPath.row] var price: Int if indexPath.section == 0 { // 対応から値段取得 Book の section だった場合 price = Book[name]! print(price) } else if indexPath.section == 1 { //... } // 合計値を出したければ、セクションごとの合計用の変数を用意しておいて足していく } } }
ソースコード
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。