LoginSignup
21
21

More than 5 years have passed since last update.

【iOS】うちの新人さんがはまったUICollectionViewController

Posted at

うちの1年目の新人さんが研修しに来てiOSアプリの開発の学習を始まりました。既存のアプリが使っていたUICollectionViewControllerの所で意外とハマってました。

  • NSFetchResultControllerとUICollectionViewControllerの連携
    • 新人さん:CoreDataの変更をどうやって自動的に画面に反映していますか?
    • 僕の答え:自動的ではなく、NSFetchResultControllerDelegateの実装によりFetchResultの変更を監視と処理ができます。
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
    atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
    newIndexPath:(NSIndexPath *)newIndexPath {
    // ここでUICollectionViewやCellの更新を行う。
}

  • UICollectionViewのperformBatchUpdatesのエラー
    • 新人さん:performBatchUpdatesでエラーが起きた、半日かけて解決できてないです。どうすればいいでしょうか?

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the collection view after the update (1) must be equal to the number of sections contained in the collection view before the update (2), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

  • 僕の答え:performBatchUpdatesを実行する前のsecion数+performBatchUpdates中に呼んだdeleteSection、insertSecionの数!=performBatchUpdates後のsection数なら、このエラーが起こります。performBatchUpdatesでdeleteSectionを一回も読んでないのに、DBからsectionのcellデータを全部削除したため、UICollectionViewの更新でsection数が変わりました。そのため、このエラーが出ました。DBからsectionのcellを全部削除したら、該当sectionを明示的にdeleteSectionしましょう。

まぁこういうような問題がありました、既存のソースが多すぎで新人さんにとって解析しにくい原因もあると思います。

21
21
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
21
21