回答編集履歴
2
コードを修正
test
CHANGED
@@ -260,7 +260,7 @@
|
|
260
260
|
|
261
261
|
// UITableViewCellで返ってくるので、CustomTableViewCellで強制キャストする
|
262
262
|
|
263
|
-
let cell
|
263
|
+
let cell = tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.reuseIdentifier, for: indexPath) as! CustomTableViewCell
|
264
264
|
|
265
265
|
|
266
266
|
|
1
カスタムのセルについて追記
test
CHANGED
@@ -121,3 +121,165 @@
|
|
121
121
|
|
122
122
|
|
123
123
|
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
# 2019/12/21追記(カスタムのセルについて)
|
128
|
+
|
129
|
+
追記ありがとうございます!
|
130
|
+
|
131
|
+
このカスタムのセルをTableViewで表示するようにするだけなので、あとはそんなにやることはないですね。
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
まずTableViewでカスタムのセルを使用する場合には、
|
136
|
+
|
137
|
+
事前にTableView側にカスタムのセルを登録する必要があるのでそれを準備します。
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
必要なのはセルの`UINib`と再利用されるときに使用されるID(`String`)なのでそれを用意します。
|
142
|
+
|
143
|
+
自分はよくセルに`static`な変数を作ってクラス名からアクセスできるようにしています。
|
144
|
+
|
145
|
+
貼っていただいたカスタムのセルだとこんな感じですね。
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
```Swift
|
150
|
+
|
151
|
+
class CustomTableViewCell: UITableViewCell {
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
@IBOutlet weak var date: UILabel!
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
// 以下の2つの定数・変数をstaticで追加
|
160
|
+
|
161
|
+
static let reuseIdentifier = "CustomTableViewCell"
|
162
|
+
|
163
|
+
static var nib: UINib {
|
164
|
+
|
165
|
+
return UINib(nibName: "CustomTableViewCell", bundle: nil)
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
override func awakeFromNib() {
|
172
|
+
|
173
|
+
super.awakeFromNib()
|
174
|
+
|
175
|
+
}
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
override func setSelected(_ selected: Bool, animated: Bool) {
|
180
|
+
|
181
|
+
super.setSelected(selected, animated: animated)
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
```
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
次にTableViewにセルを登録します。
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
```Swift
|
196
|
+
|
197
|
+
class ViewController: UIViewController {
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
// ... 省略します
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
override func viewDidLoad() {
|
206
|
+
|
207
|
+
super.viewDidLoad()
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
// TableViewのDataSourceを設定
|
212
|
+
|
213
|
+
// Storyboard側ですでに設定している場合は省略してください
|
214
|
+
|
215
|
+
tableView.dataSource = self
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
// TableViewにセルを登録します
|
220
|
+
|
221
|
+
// CustomTableViewCellにstaticでnibとreuseIdentifier定義したので以下のように
|
222
|
+
|
223
|
+
// <クラス名>.nibみたいに使用できます.
|
224
|
+
|
225
|
+
tableView.register(CustomTableViewCell.nib, forCellReuseIdentifier: CustomTableViewCell.reuseIdentifier)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
// ... 省略します
|
230
|
+
|
231
|
+
}
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
```
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
あとはデリゲートメソッド内でカスタムのセルを返すように設定するだけです。
|
240
|
+
|
241
|
+
TableViewの`dequeueReusableCell(withIdentifier:, for:)`メソッドの返し値は`UITableViewCell`になるので、
|
242
|
+
|
243
|
+
作ったカスタムセルで強制キャストして、カスタムセルにアクセスできるようにします。
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
```Swift
|
248
|
+
|
249
|
+
extension ViewController: UITableViewDataSource {
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
// ... 省略します
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
// 表示するセルの設定
|
258
|
+
|
259
|
+
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
260
|
+
|
261
|
+
// UITableViewCellで返ってくるので、CustomTableViewCellで強制キャストする
|
262
|
+
|
263
|
+
let cell.tableView.dequeueReusableCell(withIdentifier: CustomTableViewCell.reuseIdentifier, for: indexPath) as! CustomTableViewCell
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
// セルのdate(UILabel)のtextに対応するDateListの内容をセット
|
268
|
+
|
269
|
+
cell.date.text = dateList[indexPath.row]
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
return cell
|
274
|
+
|
275
|
+
}
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
```
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
こんな感じでどうでしょうか?
|