0

I have UITableViewCell and I want to send the indexPath.row number to UIViewController using didSet, but xcode gives me an error when i use the value for other things (in UIViewController) says that the value is nil error: unexpectedly found null when unwrapping an optional value. But if I print in the variable in UIViewController the value appears. What i do? Thanks.

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()

}


}
nelt
  • 11
  • Problem is that `ViewController()` is not your view controller. You might want to learn about classes and instances before you tackle object-oriented programming, which is what you're trying to do here. – matt Nov 05 '17 at 23:28
  • I want with this value drawing rectangle and the value is the height. I try to pass value directly to UIview, but I could not. – nelt Nov 05 '17 at 23:38

2 Answers2

0

ViewController() is just an instance of your ViewController. What you're doing when you say ViewController() is you're creating a new ViewController, rather than accessing the one you're looking for. If you want the indexPath.row, you need to send it to the actual VC, not just an instance of it.

LFHS
  • 295
  • 1
  • 2
  • 15
0

You need to keep in mind that not all data is loaded instantly and at the same time, also that it does not always load in the order that you expect it to. It is possible that you are asking for the data before it is even assigned. Try

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()
   }
}

I am only printing if the value is not nil! Let me know if you have more questions, or let us know if my solution is not working. =)

iOSGuy93892
  • 137
  • 7