ios - CollectionView dataSource not working when using UICollectionViewController, but works when using UIViewController with a CollectionView -


i subclassing uicollectionview, , taking care of datasource. assign datasource self during init phase of collectionview.

import foundation import uikit  class collectionviewsubclass: uicollectionview, uicollectionviewdatasource {      public override init(frame: cgrect, collectionviewlayout layout: uicollectionviewlayout) {         super.init(frame: frame, collectionviewlayout: layout)         datasource = self     }      public required init?(coder adecoder: nscoder) {         super.init(coder: adecoder)         datasource = self     }      public func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         // called first case, not second          return 1     }      public func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         // called first case, not second          return uicollectionviewcell()     } } 
  • case 1: when use collectionviewsubclass inside uiviewcontroller, works , both datasource methods (numberofitemsinsection , cellforitemat called correctly.
  • case 2: when use collectionviewsubclass inside uicollectionviewcontroller, reason datasource methods not called, although when debugged, checked init(coder:) indeed called , datasource = self got executed. seems somehow, datasource got nil again @ later stage, , datasource methods not being called.

i can't head around , tried debug, no avail. tell me reason, or give me ideas debug appreciated. thanks!

edit: when assign datasource = self after init stage, works well. rather in init phase. how can make work during init?

from uicollectionviewconrtoller docs:

when loading collection view storyboard or nib file, data source , delegate objects collection view obtained nib file. if data source or delegate not specified, collection view controller assigns itself unspecified role.

so if use uicollectionviewconrtoller need reassign datasource custom collection view.


Comments

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -