iOS 8 UICollectionViewLayout Switching Mysterious Crash in Swift -


i new working uicollectionview. trying switch uicollectionviewlayout grid list , vice versa using below code:

@ibaction func switchlayout(sender: anyobject) {     isgridlayout = !isgridlayout      collectionview?.reloaddata()      if isgridlayout {                      collectionview?.performbatchupdates({ () -> void in             self.collectionview?.collectionviewlayout.invalidatelayout()             self.collectionview?.setcollectionviewlayout(self.gridlayout, animated: true)             }, completion: { (completion) -> void in         })      } else {                      collectionview?.performbatchupdates({ () -> void in             self.collectionview?.collectionviewlayout.invalidatelayout()             self.collectionview?.setcollectionviewlayout(self.listlayout, animated: true)             }, completion: { (completion) -> void in         })                 } } 

i consistently below crash on setcollectionviewlayout:animated: method, don't understand. on fixing crash great!

terminating app due uncaught exception 'nsinvalidargumentexception', reason: '*** setobjectforkey: object cannot nil (key: <nsindexpath: 0xc000000000000016> {length = 2, path = 0 - 0})' 

the project available in github. modified form of tutorial.

i had similar problem , @ end missing implement layoutattributesforitematindexpath() in uicollectionviewlayoutattributes subclass.

this implementation

override func preparelayout() {     super.preparelayout()      if let numberofitems = self.collectionview?.numberofitemsinsection(0) {         var left: cgfloat = self.sectioninset.left         let centery = cgrectgetmidy((self.collectionview?.bounds)!)         item in 0..<numberofitems {             let itemframe = cgrect(origin: cgpoint(x: left, y: centery - (self.itemsize.height / 2)), size: self.itemsize)             let attributes = uicollectionviewlayoutattributes(forcellwithindexpath: nsindexpath(foritem: item, insection: 0))             attributes.size = item_size             attributes.frame = itemframe             attributes.alpha = 1             attributes.zindex = 1             attributes.hidden = false              self.layoutattributes.append(attributes)              left += self.itemsize.width + self.minimuminteritemspacing         }          let contentwidth: cgfloat = self.sectioninset.left + (cgfloat(numberofitems) * (self.itemsize.width + self.minimuminteritemspacing) - self.minimuminteritemspacing) + self.sectioninset.right         self.contentsize = cgsize(width: contentwidth, height: (self.collectionview?.bounds.height)!)     } }  override func layoutattributesforitematindexpath(indexpath: nsindexpath) -> uicollectionviewlayoutattributes? {     return self.layoutattributes[indexpath.item] }  override func layoutattributesforelementsinrect(rect: cgrect) -> [uicollectionviewlayoutattributes]? {     var validattributes = [uicollectionviewlayoutattributes]()     attributes in self.layoutattributes {         if cgrectintersectsrect(attributes.frame, rect) {             validattributes.append(attributes)         }     }      return validattributes }  override func collectionviewcontentsize() -> cgsize {     return self.contentsize } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -