TableViewで複数選択したものを全て取得する方法
解決したいこと
TableViewで複数選択可能にして、選択された値全てを取得して次の画面に値渡しをしたいのですが、Buildしてみると複数選択したのにもかかわらず、値が一つしか渡されませんでした。
選択された全ての値を取得(値渡し)する方法はありますでしょうか?
独学でプログラミングを始めて1ヶ月ほどなのでわかりやすく解決方法を教えてくださると嬉しいです。
発生している問題・エラー
エラーはなし
該当するソースコード
import UIKit
class SupportForm03ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource {
@IBOutlet var OS: UITableView!
@IBOutlet var NextPage: UIButton!
var rowListArray = [Int]()
var itemListArray = [Int]()
var sortArray = [Int]()
var NameSend = ""
var EmailSend = ""
var ProductSend = ""
var OSSend = ""
var ContentsSend = ""
var SolutionSend = ""
var ALLOS = ["macOS 11.2.2","macOS 11.2.1", "macOS 11.2", "macOS 11.1", "macOS 11.0.1", "macOS 11.0.1","macOS 10.15.7","macOS 10.15.6","macOS 10.15.6","macOS 10.15.6","macOS 10.15.5","macOS 10.15.5","macOS 10.15.4","macOS 10.15.4","macOS 10.15.3","macOS 10.15.2","macOS 10.15.1","macOS 10.15.0","macOS 10.15.0","macOS 10.15.0",
"iPadOS14.4","iPadOS14.3","iPadOS14.2.1","iPadOS14.2","iPadOS14.1","iPadOS14.0.1","iPadOS14","iPadOS13.7","iPadOS13.6.1","iPadOS13.6","iPadOS13.5.1","iPadOS13.5","iPadOS13.4.1","iPadOS13.4","iPadOS13.3.1","iPadOS13.3","iPadOS13.2.3","iPadOS13.2.2","iPadOS13.2","iPadOS13.1.3","iPadOS13.1.2","iPadOS13.1.1","iPadOS13.1",
"iOS14.4","iOS14.3","iOS14.2.1","iOS14.2","iOS14.1","iOS14.0.1","iOS14","iOS13.7","iOS13.6.1","iOS13.6","iOS13.5.1","iOS13.5","iOS13.4.1","iOS13.4","iOS13.3.1","iOS13.3","iOS13.2.3","iOS13.2.2","iOS13.2","iOS13.1.3","iOS13.1.2","iOS13.1.1","iOS13.1","iOS13",
"watchOS7.3.1","watchOS7.3","watchOS7.2","watchOS7.1","watchOS7.0.3","watchOS7.0.2","watchOS7.0.1","watchOS7","watchOS6.2.8","watchOS6.2.6","watchOS6.2.5","watchOS6.2.1","watchOS6.2","watchOS6.1.3","watchOS6.1.2","watchOS6.1.1","watchOS6.1","watchOS6.0.1","watchOS6",
"3.3.1","3.5.1","3.7.2","6.3.2","6.7.8","6.8.8","1A661","1A673","1A691","2A364","2B584","2B488","2C54","2D15","2D27","3A283",
"その他(不具合内容の欄で入力お願いします。)"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ALLOS.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = UITableViewCell()
cell.backgroundColor = UIColor.clear
// セルの選択時の背景色を消す
cell.selectionStyle = UITableViewCell.SelectionStyle.none
cell.textLabel?.text = ALLOS[indexPath.row]
// セルの選択状況の判定
if (rowListArray.contains(indexPath.row)){
cell.accessoryType = .checkmark
}else{
cell.accessoryType = .none
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let cell = tableView.cellForRow(at: indexPath)
// 選択したセルにチェックマークが無い場合
if(cell?.accessoryType == UITableViewCell.AccessoryType.none){
cell?.accessoryType = .checkmark
self.rowListArray.append(indexPath.row)
}else{
// 選択したセルにチェックマークがある場合
cell?.accessoryType = .none
let listNumber = rowListArray.filter ({ (n:Int) -> Bool in
if n != indexPath.row{
return true
}else{
return false
}})
rowListArray = listNumber
}
// 配列を昇順で並び替え、
// 都道府県コードに変換する。
sortArray = rowListArray.sorted{$0 < $1}
itemListArray = sortArray.map{$0 + 1}
print("OS\(itemListArray)")
}
override func viewDidLoad() {
super.viewDidLoad()
OS.allowsMultipleSelection = true
// Do any additional setup after loading the view.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! SupportForm04ViewController
vc.NameSend = NameSend
vc.EmailSend = EmailSend
vc.ProductSend = ProductSend
if let row = OS.indexPathForSelectedRow?.row {
vc.OSSend = ALLOS[row]
}
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
自分で試したこと
検索してみましたが複数の値を渡す方法が見つからず、現在に至ります。
0 likes