0
0

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 5 years have passed since last update.

tableview cell tag error viewWithTag 取得エラー対応

Last updated at Posted at 2017-06-20

最近テーブルビューを作ろうとした時にエラーの対応をしたので共有します。
Cellの中でラベルを作成し、tagと関連付けした時のエラーです。

成功例

tavleView.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = table.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        if let label10 = cell.viewWithTag(10) as? UILabel {
            label10.text = "No.\(indexPath.row + 1)"
        } else {
            print("no tag item")
        }
        return cell
}

ifで解決したらエラーは起きませんでした。
UILabelを作り出す際にnillを恐れているのかもしれません。

0
0
2

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?