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

swiftのdequeuereusablecellwithidentifierでカスタムセルを読んだ時に エラーが出た場合の対処法

Posted at

カスタムセルをswiftで利用したところハマったので忘れないうちに残しておきます。

dequeuereusablecellwithidentifierでカスタムセルを読んだ時に
exc_bad_instruction
のエラーが出た場合の対処法

以下のようにoptional型にする。

var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellId) as? UITableViewCell

if cell == nil {
  cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)
}

optional型のカスタムセルに対して、
アンラップのas?でnilを入れるようにしないといけない。

ここでoptionalにしなかったりしたりすると落ちました。

1
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
1
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?