0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift] TableViewCellのStyleをCustom以外にする

Posted at

はじめに

私はこのTableViewCellのStyleをCustom以外にした時、Cell内の部品へのアクセスの仕方が分からずに詰まりました。
今回はその解説を書いてみようと思います。
一回分かれば簡単ってやつです。

Style変更のやり方

TableViewCellを選択すると、赤枠の箇所にStyleがあるのでそこから変更できます。
スクリーンショット 2020-12-28 16.05.36.png

Cell内の部品へのアクセス

cellを生成するのでcellForRowAtの中に書いていきます。

定数cellを定義し、tableView.dequeueReusableCell(withIdentifier: , for: )を使ってcellの再利用をします。

Identifierを指定し、それに紐付いたcellを再利用することになります。

そしたら、そのcellにアクセスできるようになるので、

cell.textLabel?.text = "abc"のように部品を指定して処理を書いていきます。

このStyleの場合、左の部品名はtextLabel,右の部品名はdetailLabelとなっています。

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
        cell.textLabel?.text = "abc"
        cell.detailTextLabel?.text = "def"
        
        return cell
    }

numberOfRowsInSectionの数を4にしてみたので、

実行結果はdequeueReusableCellで指定したIdentidierのcellが4つ生成されています。
スクリーンショット 2020-12-28 16.21.46.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?