0

I want to instantiate a ViewController from a custom tableViewCell(conforming to UICollectionViewDelegate, UICollectionDatasource) class which has a custom CollectionView embedded in it.The segue needs to be performed when a particular section of the CollectionView is selected. Already tried using protocols doesnt work!

my custom TableView class :-
   import UIKit


protocol transferDelegate{

func transfer(_: Int)

}


class ParentTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource{



@IBOutlet weak var parentCellHeader: UIView!

@IBOutlet weak var feedPostUsername: UILabel!

@IBOutlet weak var feedPostUserDetails: UILabel!

@IBOutlet weak var feedPostDescription: UILabel!

@IBOutlet weak var feedPicturesCollectionView: UICollectionView!

@IBOutlet weak var feedUserProfilePictures: CustomProfilepicture!



var transferingDelegate : transferDelegate?

override func awakeFromNib() {


    super.awakeFromNib()

    feedPicturesCollectionView.dataSource = self
    feedPicturesCollectionView.delegate = self
    feedPicturesCollectionView.pagingEnabled = true
    feedPicturesCollectionView.backgroundColor = UIColor.clearColor()

}




override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

}




required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

}




func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {

    return 1

}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 10

}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) ->
    UICollectionViewCell {

    let cell = feedPicturesCollectionView.dequeueReusableCellWithReuseIdentifier("FeedPicturesCell", forIndexPath: indexPath) as! FeedPhotosCell



    switch(indexPath.row){

    case 0 :   cell.feedImages.image = UIImage(named: "defaultProfilePic")

    case 1 :   cell.feedImages.image = UIImage(named: "image1")

    case 2 :   cell.feedImages.image = UIImage(named: "image2")

    case 3 :   cell.feedImages.image = UIImage(named: "image3")


    default : cell.feedImages.image = UIImage(named: "defaultProfilePic")

        }

        return cell


}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    print(indexPath.row)

    transferingDelegate?.transfer(indexPath.row)


}


}

my viewController class : -

import UIKit

class ViewController: UIViewController , UITableViewDelegate, UITableViewDataSource,transferDelegate{

var xibName : String = "HomepageFeedCellHeader"


var lords : [String] = ["name1","name2","name3"]

var que : [String] = ["--","red","blue"]

var desc : [String] = ["abcE","defn","ghi"]

var  menProfilePictures : [UIImage] = [UIImage(named: "image1")!,UIImage(named: "image2")!,UIImage(named: "image3")!]

@IBOutlet weak var parentTableView: UITableView!


var a : ParentTableViewCell = ParentTableViewCell()






override func viewDidLoad() {

    super.viewDidLoad()



    parentTableView.delegate = self
    parentTableView.dataSource = self

    // Do any additional setup after loading the view, typically from a nib.
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lords.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = parentTableView.dequeueReusableCellWithIdentifier("ParentCell", forIndexPath: indexPath) as! ParentTableViewCell
    a.transferingDelegate = self
    cell.feedPostUsername.text = lords[indexPath.row]
    cell.feedPostUserDetails.text = queens[indexPath.row]
    cell.feedPostDescription.text = desc[indexPath.row]
    cell.feedUserProfilePictures.image = menProfilePictures[indexPath.row]

    return cell
}

func transfer(itemNo : Int) {

    print("call recieved in viewController from item \(itemNo)")

}



}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Dravidian
  • 9,945
  • 3
  • 34
  • 74
  • With the use of `NSNotificationCenter`. – Bhumit Mehta Jul 08 '16 at 16:09
  • 1
    What is `a.transferingDelegate = self`?, it must be `cell.transferingDelegate = self`. – Nirav D Jul 08 '16 at 16:20
  • @BhumitMehta i am new to this , can you please elaborate! – Dravidian Jul 08 '16 at 16:40
  • @Nirav YEAH THAT SOLVED IT.....a was `var a : ParentTableViewCell = ParentTableViewCell()` but why was i wrong? can you explain please!.. – Dravidian Jul 08 '16 at 16:41
  • 1
    While your problem is fixed I would recommand you to go through http://stackoverflow.com/questions/24049020/nsnotificationcenter-addobserver-in-swift this thread which explains how to use notification center to pass messages. Also go through this tutorial https://www.andrewcbancroft.com/2014/10/08/fundamentals-of-nsnotificationcenter-in-swift/. It will help you in future, NSNotification approach is much cleaner than delegate. – Bhumit Mehta Jul 08 '16 at 16:46
  • @BhumitMehta thanks a lot!... do you guys have a discord forum or a group or something , where you could possibly add me! Thanks in advance. – Dravidian Jul 08 '16 at 16:48
  • @AchintyaGupta you need to set the delegate to the cell of your `UITableView` so that it will call your delegate method, and that cell must be from the `cellForRowAtIndexPath`. – Nirav D Jul 08 '16 at 16:56
  • Thnaks...now i am trying to pass an image between these two ViewControllers via `imagePopOverScene?.detailImage.image = menProfilePictures[itemNo]` but it gives me an error of `fatal error: unexpectedly found nil while unwrapping an Optional value` this is my array `var menProfilePictures : [UIImage] = [UIImage(named: "image1")!,UIImage(named: "image2")!,UIImage(named: "image3")!]` – Dravidian Jul 08 '16 at 17:02
  • @AchintyaGupta Check my answer. – Nirav D Jul 08 '16 at 17:13

1 Answers1

2

Your didSelectItemAtIndexPath problem is solved. This is the answer of your second issue that you are mention in comment. You can not assign the image the imageView, if it is not in the navigation hierarchy. Your problem is that you detailImage is nil because this ViewController is not loaded in the window hierarchy. To solve your problem just do like this

imagePopOverScene?.selImage =  menProfilePictures[itemNo]

Now declare selImage in your imagePopOverScene like this

var selImage: UIImage!

Also add follwing line in your viewDidLoad of imagePopOverSceneVC

self.detailImage.image = self.selImage

This will solve your problem

Nirav D
  • 71,513
  • 12
  • 161
  • 183