質問するログイン新規登録

回答編集履歴

2

コードを修正

2019/12/21 11:36

投稿

hayabusabusash
hayabusabusash

スコア776

answer CHANGED
@@ -129,7 +129,7 @@
129
129
  // 表示するセルの設定
130
130
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
131
131
  // UITableViewCellで返ってくるので、CustomTableViewCellで強制キャストする
132
- let cell.tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.reuseIdentifier, for: indexPath) as! CustomTableViewCell
132
+ let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.reuseIdentifier, for: indexPath) as! CustomTableViewCell
133
133
 
134
134
  // セルのdate(UILabel)のtextに対応するDateListの内容をセット
135
135
  cell.date.text = dateList[indexPath.row]

1

カスタムのセルについて追記

2019/12/21 11:36

投稿

hayabusabusash
hayabusabusash

スコア776

answer CHANGED
@@ -59,4 +59,85 @@
59
59
 
60
60
  }
61
61
 
62
- ```
62
+ ```
63
+
64
+ # 2019/12/21追記(カスタムのセルについて)
65
+ 追記ありがとうございます!
66
+ このカスタムのセルをTableViewで表示するようにするだけなので、あとはそんなにやることはないですね。
67
+
68
+ まずTableViewでカスタムのセルを使用する場合には、
69
+ 事前にTableView側にカスタムのセルを登録する必要があるのでそれを準備します。
70
+
71
+ 必要なのはセルの`UINib`と再利用されるときに使用されるID(`String`)なのでそれを用意します。
72
+ 自分はよくセルに`static`な変数を作ってクラス名からアクセスできるようにしています。
73
+ 貼っていただいたカスタムのセルだとこんな感じですね。
74
+
75
+ ```Swift
76
+ class CustomTableViewCell: UITableViewCell {
77
+
78
+ @IBOutlet weak var date: UILabel!
79
+
80
+ // 以下の2つの定数・変数をstaticで追加
81
+ static let reuseIdentifier = "CustomTableViewCell"
82
+ static var nib: UINib {
83
+ return UINib(nibName: "CustomTableViewCell", bundle: nil)
84
+ }
85
+
86
+ override func awakeFromNib() {
87
+ super.awakeFromNib()
88
+ }
89
+
90
+ override func setSelected(_ selected: Bool, animated: Bool) {
91
+ super.setSelected(selected, animated: animated)
92
+ }
93
+ }
94
+ ```
95
+
96
+ 次にTableViewにセルを登録します。
97
+
98
+ ```Swift
99
+ class ViewController: UIViewController {
100
+
101
+ // ... 省略します
102
+
103
+ override func viewDidLoad() {
104
+ super.viewDidLoad()
105
+
106
+ // TableViewのDataSourceを設定
107
+ // Storyboard側ですでに設定している場合は省略してください
108
+ tableView.dataSource = self
109
+
110
+ // TableViewにセルを登録します
111
+ // CustomTableViewCellにstaticでnibとreuseIdentifier定義したので以下のように
112
+ // <クラス名>.nibみたいに使用できます.
113
+ tableView.register(CustomTableViewCell.nib, forCellReuseIdentifier: CustomTableViewCell.reuseIdentifier)
114
+
115
+ // ... 省略します
116
+ }
117
+ }
118
+ ```
119
+
120
+ あとはデリゲートメソッド内でカスタムのセルを返すように設定するだけです。
121
+ TableViewの`dequeueReusableCell(withIdentifier:, for:)`メソッドの返し値は`UITableViewCell`になるので、
122
+ 作ったカスタムセルで強制キャストして、カスタムセルにアクセスできるようにします。
123
+
124
+ ```Swift
125
+ extension ViewController: UITableViewDataSource {
126
+
127
+ // ... 省略します
128
+
129
+ // 表示するセルの設定
130
+ func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
131
+ // UITableViewCellで返ってくるので、CustomTableViewCellで強制キャストする
132
+ let cell.tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.reuseIdentifier, for: indexPath) as! CustomTableViewCell
133
+
134
+ // セルのdate(UILabel)のtextに対応するDateListの内容をセット
135
+ cell.date.text = dateList[indexPath.row]
136
+
137
+ return cell
138
+ }
139
+ }
140
+
141
+ ```
142
+
143
+ こんな感じでどうでしょうか?