TableViewCellにModelの項目を表示したいのですが、Modelの配列[string]型からstring型に変換する
configureの記述がわかりません。
TableViewCell
1import UIKit 2 3final class TableViewCell: UITableViewCell { 4 5 @IBOutlet weak var timerStart: UILabel! 6 @IBOutlet weak var timeFinish: UILabel! 7 @IBOutlet weak var syumokuLabel: UILabel! 8 @IBOutlet weak var memoLabel: UILabel! 9 10 override func prepareForReuse() { 11 super.prepareForReuse() 12 timerStart.text = nil 13 timeFinish.text = nil 14 syumokuLabel.text = nil 15 memoLabel.text = nil 16 } 17 18 func configure(menu: PickerModel) { 19 syumokuLabel.text = ここが分からない 20 21 memoLabel.text = menu.memo 22 timerStart.text = ここが分からない 23 timeFinish.text = ここが分からない 24 } 25} 26
PickerModel
1import Foundation 2 3 4struct PickerModel { 5 static var syumokuname = ["肩", "胸", "背中", "腕", "足"] 6 7 8 var startimeStr = ["1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30", "24:00", "24:30"] 9 var finishtimeStr = ["1:00", "1:30", "2:00", "2:30", "3:00", "3:30", "4:00", "4:30", "5:00", "5:30", "6:00", "6:00", "6:30", "7:00", "7:30", "8:00", "8:30", "8:00", "8:30", "9:00", "9:30", "10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00", "17:30", "18:00", "18:30", "19:00", "19:30", "20:00", "20:30", "21:00", "21:30", "22:00", "22:30", "23:00", "23:30", "24:00", "24:30"] 10 11 var memo:String = "" 12} 13
CalendarMainViewController
1import UIKit 2import FSCalendar 3 4final class CalendarMainViewController: UIViewController, FSCalendarDelegate, FSCalendarDataSource, FSCalendarDelegateAppearance { 5 6 @IBOutlet private weak var calendar: FSCalendar! 7 @IBOutlet weak var dayLabel: UILabel! 8 9 private let CELL_NIB_NAME = "TableViewCell" 10 private let CELL_ID = "TableViewCell" 11 12 private var model: [PickerModel] = [] 13 14 15 @IBOutlet private weak var tableView: UITableView! { 16 didSet { 17 let cellNIB = UINib(nibName: CELL_NIB_NAME, bundle: nil) 18 tableView.register(cellNIB, forCellReuseIdentifier: CELL_ID) 19 20 tableView.delegate = self 21 tableView.dataSource = self 22 23 } 24 } 25 26 func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) { 27 let formattar = DateFormatter() 28 formattar.dateFormat = "yyyy年MM月dd日" 29 let selectedDate = formattar.string(from: date) 30 print(selectedDate) 31 dayLabel.text = selectedDate 32 } 33 34 override func viewDidLoad() { 35 super.viewDidLoad() 36 37 self.calendar.dataSource = self 38 self.calendar.delegate = self 39 40 let myDate = Date() 41 let tempCalendar = Calendar.current 42 let nowYear = tempCalendar.component(.year, from: myDate) 43 let nowMonth = tempCalendar.component(.month, from: myDate) 44 let nowDay = tempCalendar.component(.day, from: myDate) 45 let calendar = Calendar.current 46 _ = calendar.date(from: DateComponents(year: nowYear, month: nowMonth, day: nowDay)) 47 48 } 49 50} 51 52extension CalendarMainViewController: UITableViewDataSource { 53 54 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 55 return model.count 56 57 } 58 59 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 60 guard let cell = tableView.dequeueReusableCell(withIdentifier: CELL_ID, for: indexPath) as? TableViewCell else { 61 return UITableViewCell() 62 } 63 let user = model[indexPath.row] 64 cell.configure(menu: user) 65 return cell 66 } 67 68 func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 69 return 150 70 } 71 72 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 73 tableView.deselectRow(at: indexPath as IndexPath, animated: true) 74 75 } 76 77} 78extension CalendarMainViewController: UITableViewDelegate { 79 80} 81
SecondViewController
1import UIKit 2 3class SecondViewController: UIViewController { 4 5 @IBOutlet weak var dayLabel: UILabel! 6 @IBOutlet weak var syumokuText: UITextField! 7 @IBOutlet weak var startLabel: UITextField! 8 @IBOutlet weak var finishLabel: UITextField! 9 10 private var syumokupickerView = UIPickerView() 11 private var starttimepickerView = UIPickerView() 12 private var finishtimepickerView = UIPickerView() 13 14 var model = PickerModel() 15 16 override func viewDidLoad() { 17 super.viewDidLoad() 18 createPickerView() 19 } 20 21 @IBAction func back(_ sender: Any) { 22 dismiss(animated: true, completion: nil) 23 } 24 25 private func createPickerView() { 26 // syumokupickerView 27 syumokupickerView.delegate = self 28 syumokuText.inputView = syumokupickerView 29 30 // starttimepickerView 31 starttimepickerView.delegate = self 32 startLabel.inputView = starttimepickerView 33 34 // finishtimepickerView 35 finishtimepickerView.delegate = self 36 finishLabel.inputView = finishtimepickerView 37 38 } 39 40 @objc private func donePicker() { 41 42 syumokuText.endEditing(true) 43 startLabel.endEditing(true) 44 finishLabel.endEditing(true) 45 } 46 47 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 48 syumokuText.endEditing(true) 49 startLabel.endEditing(true) 50 finishLabel.endEditing(true) 51 } 52 53 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 54 syumokuText.endEditing(true) 55 startLabel.endEditing(true) 56 finishLabel.endEditing(true) 57 } 58 59} 60 61extension SecondViewController: UIPickerViewDelegate { 62 63} 64 65extension SecondViewController: UIPickerViewDataSource { 66 67 func numberOfComponents(in pickerView: UIPickerView) -> Int { 68 return 1 69 } 70 71 func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 72 if pickerView == syumokupickerView { 73 return 5 74 } else { 75 return 50 76 } 77 } 78 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 79 if pickerView == syumokupickerView { 80 return model.syumokuname[row] 81 } else if pickerView == starttimepickerView { 82 return model.startimeStr[row] 83 } else { 84 return model.finishtimeStr[row] 85 } 86 } 87 88 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 89 if pickerView == syumokupickerView { 90 syumokuText.text = model.syumokuname[row] 91 } else if pickerView == starttimepickerView { 92 startLabel.text = model.startimeStr[row] 93 } else { 94 finishLabel.text = model.finishtimeStr[row] 95 } 96 } 97 98} 99
基礎的な部分で参考書を読んでいますが、つまづいています。
よろしくお願いします。
>configureの記述がわかりません
それ、何なんでしょうか?
func configure(menu: PickerModel) のmenuって何でしょうか?そもそもPickerModelは何のためのものなのでしょうか?TableViewCellとPickerModelがどう関係しているか分からないのですが?
説明不足ですみません…コードを追加しました。
PickerViewの配列をtableviewCell(Xib使用)に反映したいです。
menuはcellForRowAtで渡しています
まずSwiftのStructやClass (model)の作り方の基礎を学んではどうでしょうか?
または配列を保存するものが必要なのであれば、Singletonの使い方を学んだらどうでしょうか?
どういう結果にしたいんでしょうか。
modelの配列をCellのLabelに表示したいです。ご教授ありがとうございます、再度基礎から学びます。
一つの label に配列の値をぜんぶ表示したいということでしょうか? (たぶん違うと思いますけど…。)
いまいち何がしたいか分からないので、どんな画面にしたいか絵などを貼ってくれると助かります。
あなたの回答
tips
プレビュー