
Swiftで設定画面を実装する方法
Eurekaを使うよりも学習コストが低く大概既存の知識で理想的な動きを実現できるのでベタがきしたほうが早いです。でもソースの量はEurekaを使ったほうが断然少なく済みますね。
実装していないTips
セレクトされた後にセルのハイライトを消す
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
タップしたセルを取得
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("タップされたセクションの中のセルのindex番号: \(indexPath.row)")
print("タップされたセクションのindex番号: \(indexPath.section)")
説明
TableViewのStyleをGroupedに変更
cellの名前をcellに設定
このような感じになる
接続状況
実装
import UIKit
var TableTitle = [ ["menuTitle01", "title01", "title02"],
["menuTitle02", "title03", "title04"],
["menuTitle03", "menuTitle05", "menuTitle06"],
["menuTitle04", "menuTitle07"]
]
var TableSubtitle = [ ["", "subtitle02", "subtitle03"],
["","subtitle05", "subtitle06"],
["", "subtitle06", "subtitle07"],
["", "subtitle08"]
]
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override var prefersStatusBarHidden: Bool {
return true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func numberOfSections(in tableView: UITableView) -> Int {
return TableTitle.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TableTitle[section].count - 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
cell.textLabel?.text = TableTitle[indexPath.section][indexPath.row + 1]
cell.detailTextLabel?.text = TableSubtitle[indexPath.section][indexPath.row + 1]
return cell
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return TableTitle[section][0]
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
}