ios - Pass data from collectionCell inside tableCell to secondVC use protocol -


i have 2 viewcontroller's. first vc has collectioncell inside tablecell. second vc has collectioncell, , have swift file model have class. need pass data first vc (collectioncell) second vc.

pass data need collectioncell because collectioncell has images (from swift file - model). think need use protocol, don't understand how work protocol. please help.

my code:

1stvc(viewcontroller):

class viewcontroller1: uiviewcontroller {      @iboutlet weak var tableview: uitableview!      var image: [model] = []     var ref: firdatabasereference!      override func viewdidload() {         super.viewdidload()          ref = firdatabase.database().reference(withpath: "Студии2")          ref.observe(.value, with: { (snapshot) in             var newimages: [model] = []              item in snapshot.children {                 let object = model(snapshot: item as! firdatasnapshot)                 newimages.append(object)             }             self.image = newimages             self.tableview.reloaddata()         })     }     override func didreceivememorywarning() {         super.didreceivememorywarning()     } } extension viewcontroller1: uitableviewdelegate, uitableviewdatasource {     func tableview(_ tableview: uitableview, numberofrowsinsection section: int) -> int {         return image.count     }     func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {         let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! tableviewcell1         cell.images = [image[indexpath.row].image,                        image[indexpath.row].image2]         return cell     } } 

1stvc(collectioncell inside tablecell):

protocol postdelegate {     func selectedpost(cell: string) }  class tableviewcell1: uitableviewcell {      var images = [string]()     var selecetditem: model?     var postdelegate: postdelegate?  } extension tableviewcell1: uicollectionviewdelegate, uicollectionviewdatasource {     func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {          return images.count     }     func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for: indexpath) as! collectionviewcell1          cell.imagesview.sd_setimage(with: url(string: images[indexpath.item]))          return cell     }     func collectionview(_ collectionview: uicollectionview, didselectitemat indexpath: indexpath) {         postdelegate?.selectedpost(cell: self.images[indexpath.item])         print(images[indexpath.item])     } } 

2ndvc(viewcontroller):

class viewcontroller3: uiviewcontroller {      @iboutlet weak var collectionview: uicollectionview!  //    var images = [string]()     var images: [model] = []      override func viewdidload() {         super.viewdidload()     }      override func didreceivememorywarning() {         super.didreceivememorywarning()     } } extension viewcontroller3: uicollectionviewdelegate, uicollectionviewdatasource {     func collectionview(_ collectionview: uicollectionview, numberofitemsinsection section: int) -> int {         return images.count     }     func collectionview(_ collectionview: uicollectionview, cellforitemat indexpath: indexpath) -> uicollectionviewcell {         let cell = collectionview.dequeuereusablecell(withreuseidentifier: "cell", for: indexpath) as! collectionviewcell3 //        let array = [images[indexpath.item].images, //                     images[indexpath.item].images2] //        cell.imagesview.sd_setimage(with: url(string: array[indexpath.item])) //        cell.imagesview.sd_setimage(with: url(string: images[indexpath.item]))         return cell     } } 

model(swift file have class):

class model {     var image: string!     var image2: string!     var images: [string] = []     var images2: [string] = []     var ref: firdatabasereference!      init(snapshot: firdatasnapshot) {         ref = snapshot.ref         let value = snapshot.value as! nsdictionary         let snap1 = value["hall1"] as? nsdictionary         let snap2 = value["hall2"] as? nsdictionary         image = snap1?["qwerty"] as? string ?? ""         image2 = snap2?["qwerty"] as? string ?? ""         if let post1 = snap1 as? [string: anyobject] {             (_, value) in post1["images"] as! [string: anyobject] {                 self.images.append(value as! string)             }         }         if let post1 = snap1 as? [string: anyobject] {             (_, value) in post1["images"] as! [string: anyobject] {                 self.images2.append(value as! string)             }         }     } } 

pass viewcontroller1 postdelegate of tableviewcell1

func tableview(_ tableview: uitableview, cellforrowat indexpath: indexpath) -> uitableviewcell {      let cell = tableview.dequeuereusablecell(withidentifier: "cell", for: indexpath) as! tableviewcell1       //make vc1 post delegate of cell       (cell as! tableviewcell1).postdelegate = self       cell.images = [image[indexpath.row].image,                            image[indexpath.row].image2]       return cell } 

finally implement

extension viewcontroller1 : postdelegate {     func selectedpost(cell: string) {          //call self.perform segue or load vc2 , pass data here     } } 

pinch of advice :

var postdelegate: postdelegate? 

will result in holding reference delegate being passed. result in memory leaks. make safer declare delegate weak

weak var postdelegate: postdelegate? 

in order make protocol weak protocol

protocol postdelegate: nsobjectprotocol {     func selectedpost(cell: string) } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -