// when you want access to the cells from your custom function
func doSomethinWithTheCell(index: Int) {
guard let cell = collectionView.cellForItem(at: IndexPath(item: index, section: 0)) else { return }
cell.backgroundColor = .yellow
}
// ConfigViewSectionModelはSectionModel (RxDatasourcesで定義されているstruct)のtypealiasとして作成します。
typealias ConfigViewSectionModel = SectionModel<ConfigViewSection, ConfigItem>
// ConfigViewSectionはenumとして作成し、それぞれのセクションに応じて作成します。この例では"parameter", "playType", "interval"という3つのセクションに対応しています。
enum ConfigViewSection {
// number of sections as needed ...
case parameter
case playType
case interval
}
// ConfigItemもenumとして作成
enum ConfigItem {
// parameter section
case gameMode
case tempo
case timeLimit
case tries
case counts
case lowest
case highest
case pianoVolume
case metronomevolume
// type section
case ascend
case descend
case quarterNote
case eighthNote
case dyad
// interval section
case minSecond
case second
case minThird
case third
case fourth
case dimFifth
case fifth
case minSixth
case sixth
case minSeventh
case seventh
case octave
}
ViewControllerでは以下のように設定
class ConfigViewController: UIViewController {
lazy var tableView: UITableView = {
let tableView = UITableView()
// カスタムTableViewCellはいくつでもRegister可能 (Identifierで管理)
tableView.register(ConfigParameterTVCell.self, forCellReuseIdentifier: ConfigParameterTVCell.identifier)
tableView.register(ConfigSwitchTVCell.self, forCellReuseIdentifier: ConfigSwitchTVCell.identifier)
// セパレーターを消したい場合は.clearを指定
tableView.separatorColor = UIColor.clear
// AutoLayoutのためこのパラメータをfalseにする
tableView.translatesAutoresizingMaskIntoConstraints = false
return tableView
}()
lazy var datasource = RxTableViewSectionedReloadDataSource<ConfigViewSectionModel>(configureCell: configureCell)
lazy var configureCell: RxTableViewSectionedReloadDataSource<ConfigViewSectionModel>.ConfigureCell = { [weak self] (dataSource, tableView, indexPath, _) in
// indexPathによりdataSourceからitem (enum型のConfigItem)をゲットする
let item = dataSource[indexPath]
// itemをswitchする
switch item {
case .tempo:
// まず、事前にregisterしてあるCustomTableViewCell (この場合はConfigParameterTVCell) をdequeueする
let cell = tableView.dequeueReusableCell(withIdentifier: ConfigParameterTVCell.identifier, for: indexPath) as! ConfigParameterTVCell
// その後、cellの各パラメータに対して処理を記載
cell.label.text = "something something"
return cell
case .timeLimit:
// 同様にcellをdequeueし処理する
// 以下省略
}
}
class ViewController: UIViewController {
// create an instance of UITableView in a lazy fashion
lazy var tableView: UITableView = {
let tableView = UITableView()
return tableView
}()
// custom class for delegate and datasources
let customClass = CustomClass.shared
override func viewDidLoad() {
super.viewDidLoad()
// set a background color so the view is visible
view.backgroundColor = .white
// add the tableView to the view
view.addSubview(tableView)
// set the frame of the tableView to view.bounds
tableView.frame = view.bounds
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "canBeAnythingUnique")
// you can extend ViewController as the delegate and datasouces, or you can create a class for it.
tableView.delegate = customClass
tableView.dataSource = customClass
}
}
// a custom class to handle datasources and delegate methods
class CustomClass: NSObject {
// make this class singleton
public static let shared = CustomClass()
private override init() {
super.init()
}
}
// extend the CustomClass as UITableViewDatasource
extension CustomClass: UITableViewDataSource {
// two required methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// in real world scenario, you would return the count of a data array
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
// a standard UITableViewCell has a textLabel as default
cell.textLabel?.text = "Hello World!!"
return cell
}
}
extension CustomClass: UITableViewDelegate {
// any delegation methods goes here...
}
カスタムセルを作る場合は、UITableViewCellをエクステンドします。
class CustomTableViewCell: UITableViewCell {
static let identifier = "CustomTableViewCell"
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.backgroundColor = .orange
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
struct ServerResponse: Codable {
let chords: [ResponseChord]
struct ResponseChord: Codable, Hashable {
let quality: String
let voicing: String
let inversion: String
let notes: String
let root_note: Int
}
}
以下のように使用します。
guard let jsonPath = Bundle.main.path(forResource: "chords", ofType: "json") else { return }
do {
let data = try Data(contentsOf: URL(fileURLWithPath: jsonPath))
let jsonResult = try JSONDecoder().decode(ServerResponse.self, from: data)
print(jsonResult)
} catch {
}