2017-03-20 1 views
0

다음보기 컨트롤러에서 변수에 문자열을 보내는 segue를 만들려고합니다. 이 문자열은 콜렉션 뷰 셀 안에있는 레이블 내부의 텍스트이며, 레이블 안의 텍스트는 CoreData에서 가져온 것입니다. 사용자가 셀을 누르면 다음 뷰 컨트롤러로 이동하고 이전에 변수에서 선택된 셀의 텍스트를 갖게됩니다.didSelectItemAt에서 segue를 보내는 방법

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell 
    let project = projectList[indexPath.row] 

    if project.name! == "Hi " { 
      print("Yes") 
    } 
    else { 
      print("no") 
    } 

    print(project.name!) 

} 

문제는 내가이 값 (project.name를 전송하는 방법을 잘 모르겠습니다이다 : 나는이 작업을 수행하는 방법을 잘 모르겠습니다,이 선택한 셀의 텍스트를 얻을 수 있습니다 내 didSelect 기능입니다!)를 다음 뷰 컨트롤러 변수에 연결합니다.

+0

세포를 선택했을 때 segue가 수행합니까? nextViewController로 이동할 수 있습니까? –

+2

** 'cellForItemAtIndexPath' 외부에서'dequeueReusableCell'을 호출하지 마십시오 **. 데이터 소스 배열에서 데이터를 가져옵니다. – vadian

답변

1

이렇게 해보세요. 단순히 지금의

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    let vc = segue.destination as! NextViewController //Your ViewController class 
    if let cell = sender as? UICollectionViewCell, 
     let indexPath = self.collectionView.indexPath(for: cell){ 

     let project = projectList[indexPath.row] 
     print(project.name) 
     vc.name = project.name 
    } 
} 

당신의 NextViewController 유형 String의 하나 개의 속성을 만들 수 있습니다.

var name = String() 
관련 문제