2016-07-08 2 views
0

Je veux instancier une classe ViewController d'une coutume tableViewCell (conforme à UICollectionViewDelegate,UICollectionDatasource) qui a un CollectionView personnalisé intégré dans it.The Segue doit être effectuée lorsqu'une section particulière de la CollectionView est sélectionnée. Déjà essayé en utilisant des protocoles ne fonctionne pas!Comment instancier un ViewController d'un didSelectItemAtIndexPath d'un CollectionViewController personnalisé qui fait partie d'un tableViewCell

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) 


} 


} 

ma classe viewController: -

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)") 

} 



} 
+0

Avec L'utilisation de 'NSNotificationCenter'. –

+1

Qu'est-ce que 'a.transferingDelegate = self' ?, il doit s'agir de' cell.transferingDelegate = self'. –

+0

@BhumitMehta je suis nouveau à cela, pouvez-vous élaborer s'il vous plaît! – Dravidian

Répondre

2

Votre problème est résolu didSelectItemAtIndexPath. C'est la réponse de votre deuxième numéro que vous mentionnez dans le commentaire. Vous ne pouvez pas affecter l'image imageView si elle n'appartient pas à la hiérarchie navigation. Votre problème est que detailImage est nul car ce ViewController n'est pas chargé dans la hiérarchie de fenêtre. Pour résoudre votre problème le faire comme ça

imagePopOverScene?.selImage = menProfilePictures[itemNo] 

Maintenant déclarer selImage dans votre imagePopOverScene comme celui-ci

var selImage: UIImage! 

ajouter également la ligne follwing dans votre viewDidLoad de imagePopOverSceneVC

self.detailImage.image = self.selImage 

Cela résoudra votre problème

+0

Merci, vous êtes un sauveteur! .... – Dravidian