TableViewをセクションによって複数選択可能にしたいのですが、両セクション共、複数選択可能になってしまいます。誤っている箇所をご指摘いただけましたら幸いです。よろしくお願いいたします。
import UIKit class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet var tableView: UITableView! var sectionName = ["country", "city"] var array = ["USA", "Italy", "France"] var array1 = ["New York", "Milano", "Paris"] override func viewDidLoad() { super.viewDidLoad() tableView = self tableView = self } func numberOfSections(in tableView: UITableView) -> Int { return sectionName.count } func tableView(_ tableView: UITableView,titleForHeaderInSection section: Int) -> String? { return sectionName[section] } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if section == 0 { return array.count } else if section == 1 { return array1.count } else{ return 0 } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView(withIdentifier: "cell", for: indexPath) switch indexPath.section { case 0: cell.textLabel?.text = array[indexPath.row] case 1: cell.textLabel?.text = array1[indexPath.row] default: break } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let index = tableVIew.indexPathForSelectedRow let currentCell = tableVIew.cellForRow(at: index!) switch indexPath.section { case 0: tableVIew.allowsMultipleSelection = false currentCell?.accessoryType = .checkmark case 1: tableVIew.allowsMultipleSelection = true currentCell?.accessoryType = .checkmark default: break } } }
回答2件
あなたの回答
tips
プレビュー