2016-07-08 7 views
0

の一部であるカスタムCollectionViewControllerのdidSelectItemAtIndexPathからのViewControllerをインスタンス化する方法it.Theセグエに埋め込まれたカスタムCollectionViewを持つクラス(UICollectionViewDelegate,UICollectionDatasourceに準拠します) CollectionViewの特定のセクションが選択されたときに実行する必要があります。すでにプロトコルを使用して試してみました!は、私は、カスタム<code>tableViewCell</code>から<code>ViewController</code>をインスタンス化したい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) 


} 


} 

私の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

とNSNotificationCenterの使用。 –

+1

'a.transferingDelegate = self'とは何ですか?それは' cell.transferingDelegate = self'でなければなりません。 –

+0

@BhumitMehta私はこれに新しいです、あなたは精巧にお聞かせください! – Dravidian

答えて

2

あなたdidSelectItemAtIndexPath問題が解決されます。これはあなたがコメントで言及している2番目の問題の答えです。 階層にない場合は、画像をimageViewに割り当てることはできません。あなたの問題は、このViewControllerがウィンドウ階層にロードされていないため、detailImageがnilであることです。あなたの問題を解決するためには、単に

このような
imagePopOverScene?.selImage = menProfilePictures[itemNo] 

をやっていることは今これがあなたを解決します。また、あなたのviewDidLoad

imagePopOverSceneVC
self.detailImage.image = self.selImage 

でfollwing行を追加するには、imagePopOverScene

var selImage: UIImage! 

このように selImageを宣言します問題

+0

ありがとう、あなたは命を救う人です! – Dravidian

関連する問題