2016-09-13 7 views
0

미친 날이 며칠 지나면 내 문제의 해결책을 찾을 수 없었습니다. 문제는 다음과 같습니다. UICollectionView에서 셀을 선택한 후 didSelectItemAtIndexPath 메서드가 호출되지 않습니다.didSelectItemAtIndexPath of UiCollectionView가 호출되지 않았습니다.

내보기 구조는 다음이보기는 제어기에 의해 관리된다

Views structure

다음있다 방법 :

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    initCategoriesCollection() 

    // Do any additional setup after loading the view. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

func initCategoriesCollection(){ 
    let ccVC : CategoriesCollectionViewController = UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController; 
    addChildViewController(ccVC) 
    ccVC.view.frame = CGRectMake(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height); 
    containerView.addSubview(ccVC.view) 

    ccVC.didMoveToParentViewController(self) 

}  

용기 뷰 상기 구조는 다음 :

enter image description here

그리고이 컨테이너보기 이러한 방법을 구현 이는의 ViewController에 의해 관리됩니다 :

// tell the collection view how many cells to make 
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.e1Categories.count 
} 

// make a cell for each cell index path 
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    // get a reference to our storyboard cell 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CategoryCollectionViewCell 

    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.categoryName.text = self.e1Categories[indexPath.item].string 
    cell.categoryName.numberOfLines = 0 
    cell.categoryName.sizeToFit() 
    cell.categoryName.textAlignment = .Center 
    cell.categoryCircle.makeCircle() 

    return cell 
} 

// MARK: - UICollectionViewDelegate protocol 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    // handle tap events 
    print("You selected cell #\(indexPath.item)!") 
} 

사람이 무슨 일이 일어나고 있는지 알고 있나요? 터치 이벤트를 가로 챌 수도 있기 때문에 ScrollView를 제거하려고했지만 아직 작동하지 않습니다. 여기 Stackoverflow에서 다른 질문을 읽은 후 그 중 아무 것도 나를위한 해결책이 없습니다.

미리 감사드립니다.

편집 :

내가 연결 한 내 delegate 당신이 아래의 그림에서 볼 수있는 스토리 보드를 통해 dataSource : 당신의 initCategoriesCollection()에서

enter image description here

+1

에 '대리인'이 연결되어 있습니까? – holex

+1

'ccVC.delegate = self'를 추가하면'initCategoriesCollection' 메쏘드에서 트릭을해야합니다 – EBDOKUM

+0

@holex 예, 스토리 보드를 통해 연결했습니다. 나는 그것의 포획을 가진 포스트를 편집했다. @EBDOKUM 다음 문제를 제기하면 '값 유형'CategoriesCollectionViewController '에'delegate '멤버가 없습니다. – kikettas

답변

0

먼저 내 문제를 해결할 수있는 힌트를 주셔서 감사합니다. 부모 컨트롤러를 으로 할당하려고하면 자식 뷰 컨트롤러에 delegate이없는 것으로 나타났습니다. 내 문제는 내가 delegatedataSource을 통해 Storyboard을 할당하면 문제가 해결 될 것이라고 생각했기 때문입니다.

그래서 Delegate 및 Datasource 프로토콜을 상위 컨트롤러에 구현하고 하위 컨트롤러를 제거하기로 결정했습니다. 이제는 매력처럼 작동합니다. 어쩌면 내가 Storyboard을 사용할 때 개념을 오해하고 있었을 수도 있습니다. 당신의 도움을 주셔서 감사합니다!

1

, 당신은 새로운 CategoriesCollectionViewController을 만드는 :

let ccVC : CategoriesCollectionViewController = UIStoryboard(name:"CandidateProfile",bundle:nil).instantiateViewControllerWithIdentifier("categoriesController") as! CategoriesCollectionViewController; 

당신에 따르면 :

그리고이 컨테이너보기는 이러한 방법을 구현하는의 ViewController에 의해 관리됩니다 :

그런 다음 당신이 delegate FUNC didSelectItemAtIndexPath를 구현하는 또 다른 controllerccVC 추가됩니다. 따라서 스토리 보드에 delegate 세트가 올바르게 관리되고 있지 않습니다. controller

구현중인 다른 controller에 추가하려고하므로 delegate을 업데이트해야합니다. 이 업데이트를 사용해보십시오

관련 문제