Swiftで、UICollectionViewCellのviewWithTagで返ってくるUIViewがnilの原因


Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 2692

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798
LINEで送る
Pocket

UICollectionViewCellのデザインなんかは、Storyboardで済ますことが多いと思います。ぼくの場合は、画像表示のUIImageViewやUILabelなどを配置して、再利用のIdentifierを設定します。

この時に、それぞれ配置したUIViewに対してTagを設定しておくことで、collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!メソッド内で、viewWithTagを呼び出してデータの内容と結びつけるようにしています。

Objective-CのときもそうでしたしSwiftでも同様です。

でも、Swiftでコードを書いていると何だかCellのviewWithTagがちゃんと動作していないような動きをしていました。
調べてみると、同じような状況の人もいるようだったのですが、たぶん対応可能な問題だろうと思いました。

ios – viewWithTag in UICollectionViewCell returns nil in Swift (until cell is reused) – Stack Overflow

で、色々と試したのですがStoryboardで設定したCellをちゃんと取得していないのが原因だと推測できる動きをしていました。


よくよくコードを読み返すと、SwiftでUICollectionViewControllerのサブクラスを作るときに自動生成されていたコードが、Objective-Cのものと若干違うのが原因でした。

override func viewDidLoad() {
    super.viewDidLoad()

    // Register cell classes
    // これが不要
    self.collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

}

UICollectionViewCellを再利用できるように事前に登録するコードなのですが、これはStoryboardを使うと自動で設定されるので、不要です。

この辺りの登録用のメソッドは、xibファイルなどで、別途UICollectionViewCellのサブクラスとUIを作っている場合などに使います。

それだけならまだ問題がなかったのですが、ぼくは1種類しかCellの種類がない場合には、”Cell”という名前で、ほとんどのIdentifierを設定していて、これが上記のreuseIdentifierとして設定されていたので、影響を受けてしまいました。

バグだと思い込んで終わらせなくてよかったです。
原因が納得できたので、ホッとしました。

これからも更新を続けていきますので、Feedlyへの登録をお願いします!

follow us in feedly

LINEで送る
Pocket


Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

コメントを残す
Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/jagaimopotato/www/html/blog/wp-includes/formatting.php on line 4798