ios - Creating UICollectionView Programatically -


i learning how create uicollectionview programatically. want create grid of pictures collected user in part of app. sample code me accomplish this? also, how configure data emit image want? image file shown below, source code uicollectionview.

uicollectionview:

    class photosviewcontroller: uiviewcontroller, uicollectionviewdelegate, uicollectionviewdatasource { override func viewdidload() {     super.viewdidload()     let imagestore = imagestore() }  override func viewwillappear(animated: bool) {     super.viewwillappear(animated)      let layout: uicollectionviewflowlayout = uicollectionviewflowlayout()     layout.sectioninset = uiedgeinsets(top: 20, left: 10, bottom: 10, right: 10)     layout.itemsize = cgsize(width: 100, height: 100)      let mycollectionview:uicollectionview = uicollectionview(frame: self.view.frame, collectionviewlayout: layout)     mycollectionview.datasource = self     mycollectionview.delegate = self     mycollectionview.registerclass(rdcellcollectionviewcell.self, forcellwithreuseidentifier: "mycell")     mycollectionview.backgroundcolor = uicolor.whitecolor()     self.view.addsubview(mycollectionview) }  func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {     return images.count }  var images: [uiimage] = [  ]  func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {     let mycell = collectionview.dequeuereusablecellwithreuseidentifier("mycell", forindexpath: indexpath) as! rdcellcollectionviewcell     mycell.imageview.image = images[indexpath.item]     mycell.backgroundcolor = uicolor.graycolor()     return mycell }  func collectionview(collectionview: uicollectionview, didselectitematindexpath indexpath: nsindexpath) {     print("user tapped on item \(indexpath.row)") }   override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. } 

}

imagestore.swift:

    class imagestore: nsobject {  let cache = nscache()  func setimage(image: uiimage, forkey key: string) {     cache.setobject(image, forkey: key)      let imageurl = imageurlforkey(key)      if let data = uiimagejpegrepresentation(image, 0.5) {         data.writetourl(imageurl, atomically: true)     } } func imageforkey(key: string) -> uiimage? {     if let existingimage = cache.objectforkey(key) as? uiimage {         return existingimage     }      let imageurl = imageurlforkey(key)     guard let imagefromdisk = uiimage(contentsoffile: imageurl.path!) else {         return nil     }      cache.setobject(imagefromdisk, forkey: key)     return imagefromdisk }  func deleteimageforkey(key: string) {     cache.removeobjectforkey(key)      let imageurl = imageurlforkey(key)     {         try nsfilemanager.defaultmanager().removeitematurl(imageurl)     }     catch let deleteerror {         print("error removing image disk: \(deleteerror)")     } }  func imageurlforkey(key: string) -> nsurl {     let documentsdirectories =     nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask)     let documentdirectory = documentsdirectories.first!      return documentdirectory.urlbyappendingpathcomponent(key) } 

}

you're on right track. you'll need create subclass of uicollectionviewcell contains uiimageview; let plug correct uiimage in cellforitematindexpath.

this describes how hook custom cell: create uicollectionviewcell programmatically without nib or storyboard

as getting correct image, you'll need map index path image store somehow, item number corresponds correct image key.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -