2016-11-05 6 views
0

내부 사용자의있는 tableView에서 나는 두 개의 클래스가 있습니다.는 SEGUE 프로그래밍 방식 collectionView

tableViewCell을 클릭하면 사용자 정의 단편을 만들려고합니다. tableView (didSelectRowAt) 함수를 잘 작동하는 tableView 구현했습니다. 그러나, 나는이 함수에서 performSegue를 호출 할 수 없다. 누구든지이 문제를 해결하는 방법을 알고 있습니까?

답변

0

클래스 eventsCustomCollectionCell이 UIViewController 하위 클래스가 아닌 UICollectionViewCell 하위 클래스이기 때문입니다.

func performSegue(withIdentifier identifier: String, sender: Any?) 

은 UIViewController에서 사용할 수있는 방법입니다. 이 솔루션에 대해이 프로토콜을 구현할 수있는 방법

func cellClicked(cell: eventsCustomCollectionCell) 

하고 FeedAndGroupsViewController을 가질 수 있고 performSegue를 호출 할 수 있습니다 eventsCustomCollectionCell의 프로토콜을 만들 수 있습니다.

이 코드를 참조하고 시작할 수있는 유스 케이스에 대한 스켈레톤 코드를 작성했습니다.

class EventsCustomCollectionCell: UICollectionViewCell,UITableViewDelegate{ 
    weak var delegate : EventsCustomCollectionCellDelegate? 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     delegate?.didClick(cell: self) 
    } 
} 

protocol EventsCustomCollectionCellDelegate : class{ 
    func didClick(cell:EventsCustomCollectionCell) 
} 

class FeedAndGroupsViewController: UIViewController,EventsCustomCollectionCellDelegate,UICollectionViewDataSource{ 
var collectionView : UICollectionView! 
var yourArrayForCollectionView = [String]() 

func didClick(cell:EventsCustomCollectionCell){ 
    if let index = collectionView.indexPath(for:cell){ 
     let object = yourArrayForCollectionView[index.row] 
     performSegue(withIdentifier: "Your segue identifier", sender: object) 
    } 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{ 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Your cell reuse id", for: indexPath) as! EventsCustomCollectionCell 
    cell.delegate = self 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 
    return yourArrayForCollectionView.count 
} 
} 

희망이 도움이됩니다.

+0

감사합니다! 귀하의 솔루션을 시도했지만 cellForItemAt 함수에서 collectionViewCell의 대리자를 호출 할 수없는 것 같습니다. –

+0

UICollectionViewCell에서 UITableViewDelegate의 didSelectRowAt에서 대리자 메서드를 호출해야합니다. 내 코드를 업데이트했습니다. –

+0

좋아요! 대단히 감사합니다. –

0

이것은 내 바퀴를 얼마 동안 돌렸다. 내 경우에는 UICollectionView 내부에 UITableViewCell이 있습니다. 테이블 뷰 셀에 항목 배열이 있습니다. ViewController 내부의 지정된 인덱스에 대한 항목에 액세스 할 수 없었습니다. 또한 스토리 보드를 사용하지 않으므로 performSegue(withIdentifier: "Your segue identifier", sender: object)을 사용했습니다. self.navigationController?.pushViewController(detailVC, animated: true)

다음은 저에게 맞았습니다. 실제로 프로토콜 함수에서 객체를 전달해야했습니다. 의 ViewController에서

protocol NearbyTableViewCellDelegate : class{ 
    func didClick(cell: EatsItemNearbyTableCell, eat: Eats) 
} 


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     let eat = nearByEats[indexPath.row] 
     delegate?.didClick(cell: self, eat: eat) 
    } 

: 당신의 도움에 대한

extension EatsItemDetailVC: NearbyTableViewCellDelegate { 
    func didClick(cell: EatsItemNearbyTableCell, eat: Eats) { 
     let detailVC = EatsItemDetailVC() 
     detailVC.eat = eat 
     detailVC.currentLocation = currentLocation 
     self.navigationController?.pushViewController(detailVC, animated: true) 
    } 
}