前提・実現したいこと
テーブルビューのセルを選択して、セクションごとの合計金額を次の画面で表示させたい。
現在、カテゴリー別に商品の合計金額を計算するアプリを作っています。商品を画面に表示させる事と、セルを複数選択することはできたのですが、セルを選択したときに値段を取得する方法が全くわかりませんでした。わかりやすく説明していただけるとありがたいです。お願いいたします。
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
import UIKit
class TableViewController: UITableViewController {
var mySections = String
var twoDimArray = [[String]]() 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 } // セルが選択された時に呼び出される override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at:indexPath) }
// セルの選択が外れた時に呼び出される
override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at:indexPath)
}
}
ソースコード
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/09/04 04:09
2019/09/04 04:18
2019/09/04 04:30