2017-11-05 3 views
0

UITableViewCell이 있고 didSet을 사용하여 UIViewController에 indexPath.row 번호를 보내려하지만 xcode는 다른 것들 (UIViewController에서)의 값을 사용할 때 오류가 발생합니다. nil 오류 : 선택적 값의 래핑을 할 때 예기치 않게 null이 발견되었습니다. 그러나 UIViewController 변수에 인쇄 할 경우 값이 나타납니다. 나는 무엇을 하는가? 감사.UITableViewCell에서 UIViewController로 데이터 전달

class TableViewCellCentral: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource { 


@IBOutlet weak var CollectionData: UICollectionView! 


var send = Int() { 
    didSet{ 

     ViewController().reload = send 

     } 
} 


override func awakeFromNib() { 
    super.awakeFromNib() 

    CollectionData.dataSource = self 
    CollectionData.delegate = self 

    CollectionData.reloadData() 
} 


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


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = CollectionData.dequeueReusableCell(withReuseIdentifier: "CellData", for: indexPath) as! CollectionViewCellData 

    cell.LabelData.text! = "number \(indexPath.row)" 

    return cell 
} 



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = CollectionData.dequeueReusableCell(withReuseIdentifier: "CellData", for: indexPath) as! CollectionViewCellData 

    send = indexPath.row 

    CollectionData.reloadData() 

} 


} 
+0

문제는'ViewController()'가보기 컨트롤러가 아닙니다. 여기에서하려고하는 것은 객체 지향 프로그래밍을 다루기 전에 클래스와 인스턴스에 대해 배우고 싶을 수도 있습니다. – matt

+0

이 값 그리기 직사각형을 사용하고 싶습니다. 값은 높이입니다. 나는 UIview에 직접 값을 전달하려고 시도하지만 그렇게 할 수는 없습니다. – nelt

답변

0

ViewController()은 단지 ViewController의 인스턴스입니다. ViewController()라고 할 때 찾고있는 것은 찾고있는 뷰에 액세스하는 대신 새로운 ViewController를 작성하는 것입니다. indexPath.row를 원한다면 인스턴스 만이 아니라 실제 VC로 보내야합니다.

0

모든 데이터가 즉시로드되는 것은 아니며 동시에 예상 한 순서대로로드되지 않을 수도 있음을 명심해야합니다. 데이터가 할당되기 전에 데이터를 요청할 수도 있습니다. 시도해보십시오.

import UIKit 
class TableViewCellCentral: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource { 


@IBOutlet weak var CollectionData: UICollectionView! 


var send = Int() { 
    didSet { 
     ViewController().reload = send 
    } 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 

    CollectionData.dataSource = self 
    CollectionData.delegate = self 
    CollectionData.reloadData() 
} 

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = CollectionData.dequeueReusableCell(withReuseIdentifier: "CellData", for: indexPath) as! CollectionViewCellData 
    if indexPath.row != nil { 
     cell.LabelData.text! = "number \(self.indexPath.row)" 
    } 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
    let cell = CollectionData.dequeueReusableCell(withReuseIdentifier: "CellData", for: indexPath) as! CollectionViewCellData 
    send = indexPath.row 
    CollectionData.reloadData() 
    } 
} 

값이 0이 아닌 경우에만 인쇄하고 있습니다. 더 궁금하신 점이 있으면 알려주거나 솔루션이 작동하지 않는 경우 알려주십시오. =)

관련 문제