ios - cellForItemAtIndexPath indexPath not called for multiple collection views in a single UIViewController -
i trying have multiple collection views in single view controller. have tried achieve using separate tags each collection views, seems me celforitematindexpath
not being invoked. following code uiviewcontroller
.
import uikit class homeviewcontroller: uiviewcontroller { // mark: - iboutlets @iboutlet weak var backgroundimageview: uiimageview! @iboutlet weak var collectionview: uicollectionview! @iboutlet weak var friendscollectionview: uicollectionview! // mark: - uicollectionviewdatasource private var interests = interest.createinterests() private var friends = friends.createfriends() override func preferredstatusbarstyle() -> uistatusbarstyle { return .lightcontent } override func viewdidload() { super.viewdidload() self.collectionview.tag = 100; self.friendscollectionview.tag = 200; collectionview.registerclass(nsclassfromstring("interestcell"),forcellwithreuseidentifier:"cell"); friendscollectionview.registerclass(nsclassfromstring("friendcell"),forcellwithreuseidentifier:"cellb"); self.view.addsubview(collectionview) self.view.addsubview(friendscollectionview) print("view did load") } private struct storyboard { static let cellidentifier = "interestcell" static let friendidentifier = "friendcell" } } extension homeviewcontroller : uicollectionviewdatasource { func numberofsectionsincollectionview(collectionview: uicollectionview) -> int { print("number of sections 1") return 1 } func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell { print("trying this") if collectionview.tag == 100 { let cell = collectionview.dequeuereusablecellwithreuseidentifier(storyboard.cellidentifier, forindexpath: indexpath) as! interestcollectionviewcell print("inside pluto") return cell } else { let cellb = friendscollectionview.dequeuereusablecellwithreuseidentifier(storyboard.friendidentifier, forindexpath: indexpath) as! friendlistcollectionviewcell print("inside venus") return cellb } } func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int { print("hello") if collectionview == collectionview { print(interests.count) return interests.count } else { print("hmmm") return friends.count } } }
can please suggest i'm doing wrong?
to reach cellforitematindexpath method, have set collectionview datasource :
collectionview.datasource = self; friendscollectionview.datasource = self;
also, better instead of using tags recognize each collection, using similar :
if (collectionview == self.collectionview) { } else if (collectionview == self.friendscollectionview) { // init friend cell ... }
Comments
Post a Comment