ios - Segue all the data of a TableView to a CollectionView swift parse -


in order limit number of query database , consumption of data , battery, segue data of tableview collectionview.

import uikit import parseui import parse  class listcontrollerview: pfquerytableviewcontroller {     // initialise pfquerytable tableview    override init(style: uitableviewstyle, classname: string!)     {       super.init(style: style, classname: classname)    }    required init(coder adecoder: nscoder)      {        super.init(coder: adecoder)!         // configure pfquerytableview        self.parseclassname = "shopz"        self.textkey = "shopname"        self.pulltorefreshenabled = true        self.paginationenabled = false     }     // define query provide data table view    override func queryfortable() -> pfquery {       var query = pfquery(classname: "shopz")       // query.orderbyascending("updatedat")       return query    }     //override func tableview(tableview: uitableview,       cellforrowatindexpath indexpath: nsindexpath) -> uitableviewcell    override func tableview(tableview: uitableview, cellforrowatindexpath indexpath: nsindexpath, object: pfobject?) -> pftableviewcell {      var cell = tableview.dequeuereusablecellwithidentifier("cell") as! listcontrollerviewcell!     if cell == nil {         cell = listcontrollerviewcell(style: uitableviewcellstyle.default, reuseidentifier: "cell")     }      // display flag image     if let finalimage = object?["imagepromo"] as? pffile     {         finalimage.getdatainbackgroundwithblock             {(imagedata: nsdata?, error: nserror?) -> void in             if error == nil             {                 if let imagedata = imagedata                 {                     cell.imagepromo!.image = uiimage(data:imagedata)                 }             }            }     }      // extract values pfobject display in table cell     if let shopnamelabel = object?["shopname"] as? string     {         cell.shopname!.text = shopnamelabel         print(shopnamelabel)     }      if let shopaddresslabel = object?["shopaddress"] as? string     {         cell.shopaddress!.text = shopaddresslabel         print(shopaddresslabel)     }            return cell;  }   // in storyboard-based application, want little preparation before navigation     override func prepareforsegue(segue: uistoryboardsegue, sender: anyobject?) {      // new view controller using [segue destinationviewcontroller].     var detailscene = segue.destinationviewcontroller as! collectionview                  detailscene.shopzcollection = object pfobject?     }      override func viewdidappear(animated: bool) {          // refresh table ensure data changes displayed         tableview.reloaddata();    } }` 

collection view controller:

import uikit import parse import parseui  var shopzcollection = [pfobject]()`  class collectionview: uiviewcontroller, uicollectionviewdatasource, uicollectionviewdelegate {   // connection collection view @iboutlet weak var collectionview: uicollectionview!  override func viewdidload() {     super.viewdidload()  }  /* ========================================================================================== ensure data within collection view updated when ever displayed ========================================================================================== */  // load data collectionview when view appears override func viewdidappear(animated: bool) {     loadcollectionviewdata() }  /* ========================================================================================== fetch data parse platform ========================================================================================== */  func loadcollectionviewdata() {      // build parse query object     var query = pfquery(classname:"shopz")       // fetch data parse platform     query.findobjectsinbackgroundwithblock {         (objects: [pfobject]?, error: nserror?) -> void in          // find succeeded rocess found objects countries array         if error == nil {              // clear existing data             shopzcollection.removeall(keepcapacity: true)              // add shop objects our array             if let objects = objects [pfobject]? {                 shopzcollection = array(objects.generate())             }              // reload our data collection view             self.collectionview.reloaddata()          } else {             // log details of failure             print("error: \(error!) ")         }     } }  /* ========================================================================================== uicollectionview protocol required methods ========================================================================================== */  func numberofsectionsincollectionview(collectionview: uicollectionview) -> int {     return 1 }  func collectionview(collectionview: uicollectionview, numberofitemsinsection section: int) -> int {     return shopzcollection.count }  func collectionview(collectionview: uicollectionview, cellforitematindexpath indexpath: nsindexpath) -> uicollectionviewcell {      let cell = collectionview.dequeuereusablecellwithreuseidentifier("collectioncell", forindexpath: indexpath) as! collectionviewcell      // display country name     if let value = shopzcollection[indexpath.row]["shopname"] as? string {         cell.celltitle.text = value     }      if let value = shopzcollection[indexpath.row]["shopaddress"] as? string {         cell.celldetail.text = value     }      // fetch final flag image - if exists      if let finalimage = shopzcollection[indexpath.row]["imagepromo"] as? pffile {          finalimage.getdatainbackgroundwithblock             {(imagedata: nsdata?, error: nserror?) -> void in                 if error == nil                 {                     if let imagedata = imagedata                     {                         cell.cellimage!.image = uiimage(data:imagedata)                     }                 }         }     }      return cell }     /* ========================================================================================== process memory issues completed ========================================================================================== */  override func didreceivememorywarning() {     super.didreceivememorywarning()     // dispose of resources can recreated. }    

}`

i guess have think again structure of code/project:

  • create swift file var ( shopname, shopaddress, imagepromo)

  • create wife file query data ( query.findobjectsinbackgroundwithblock) function call involves change list controller

  • file list

  • file collection
    don't know how call function executes query...

in prepareforsegue of tableviewcontroller, trying pass 1 object (detailscene.shopzcollection = object pfobject?).

you should pass entire collection of objects (self.objects) collectionview.

i'm not versed swift, obj-c equivalent be:

- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender {     ....     detailscene.shopzcollection = self.objects;   // shopzcollection nsarray in destination } 

Comments