2016-07-08 3 views
0

의 일부 사용자 정의 CollectionViewController의 didSelectItemAtIndexPath에서의 ViewController를 인스턴스화하는 방법 좋았지 SEGUE에 포함 된 사용자 정의 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 문제가 해결된다. 이것은 당신이 논평에서 언급하고있는 두 번째 이슈의 해답입니다. 계층 구조에 있지 않은 경우 imageView 이미지를 할당 할 수 없습니다. 이 ViewController이 창 계층 구조에로드되어 있지 않으므로 detailImage은 nil입니다. 단지 또한 imagePopOverSceneVC

self.detailImage.image = self.selImage 

이 해결됩니다 viewDidLoad의에서 follwing을 줄을 추가 지금이

var selImage: UIImage! 

처럼 imagePopOverSceneselImage 선언이

imagePopOverScene?.selImage = menProfilePictures[itemNo] 

좋아해요 문제를 해결하기 위해 당신의 문제

+0

감사합니다. 당신은 생명의 은인입니다! .... – Dravidian

관련 문제