Created
March 23, 2019 15:20
-
-
Save churabou/9551fa7596d1f8ec7a4d761e264a94d0 to your computer and use it in GitHub Desktop.
設定画面を2分で作るやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class Example: UIViewController { | |
private var sections = [ | |
["設定①", "設定②", "設定③"], | |
["使い方", "有料版"], | |
["バージョン1.0.0"] | |
] | |
func onSelectRow(_ indexPath: IndexPath) { | |
print(sections[indexPath.section][indexPath.row]) | |
} | |
override func loadView() { | |
view = SettingTable( | |
sections: sections, | |
onSelectRow: onSelectRow | |
) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class SettingTable: UITableView { | |
typealias SelectHandler = (IndexPath) -> () | |
convenience init(sections: [[String]], onSelectRow: @escaping SelectHandler) { | |
self.init(frame: .zero, style: .grouped) | |
self.sections = sections | |
self.register(UITableViewCell.self, forCellReuseIdentifier: "cell") | |
self.selectHandler = onSelectRow | |
self.delegate = self | |
self.dataSource = self | |
} | |
private var sections: [[String]] = [] | |
private var selectHandler: SelectHandler? | |
} | |
extension SettingTable: UITableViewDataSource { | |
func numberOfSections(in tableView: UITableView) -> Int { | |
return sections.count | |
} | |
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return sections[section].count | |
} | |
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) | |
cell.textLabel?.text = sections[indexPath.section][indexPath.row] | |
cell.selectionStyle = .none | |
return cell | |
} | |
} | |
extension SettingTable: UITableViewDelegate { | |
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { | |
selectHandler?(indexPath) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment