3

i spend lot of time .. but i can't figure out why it is not working .. cell for index path called and set value properly but cell class i found nil value .. if your need any information then let me know .

in my collectionview index path

enter image description here

in collectionview cell

enter image description here

here is my code :

for collectionview :

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: menuBarItemDetailId, for: indexPath) as! MenuBarItemDetail
        cell.shopCategory = "600"
        print(cell.shopCategory)
        return cell
    }

for cell :

class MenuBarItemDetail: UICollectionViewCell , UITableViewDataSource , UITableViewDelegate {
    var shopCategory : String?

    override init(frame: CGRect) {
        super.init(frame: frame)

         print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
}
cristan lika
  • 415
  • 1
  • 7
  • 22
  • it is taken optional value use print(cell.shopCategory)! – Lalit kumar Jan 17 '17 at 10:37
  • yes ! I already done that one please check 1st image but no result .. do you meant that one – cristan lika Jan 17 '17 at 10:40
  • @Lalitkumar yes optional value use that as my first picture – cristan lika Jan 17 '17 at 10:42
  • @cristanlika no need of these in cell class UITableViewDataSource , UITableViewDelegate at first because this is collection view cell so all delegates you need to put in your view controller and remove init function. no need. you can use awakeFromNib – Sagar Snehi Jan 17 '17 at 10:57

2 Answers2

5

Because your awakeFromNib method called first and then cellForRow .You are assigning the value of variable in cellForRow so when first project executes its value is nil.

Solution

  1. your variable in your custom cellclass

    var myVar : String?
    
  2. Create a method in your custom cellclass

    func myMethod(str : String){
        myVar = str
        print("var : \(myVar)")        
    }
    
  3. in cellForItem, call your function like this

    cell.myMethod(str: "343")
    

Output

enter image description here

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
3

When you are writing

let cell = collectionView.dequeueReusableCell(withReuseId`entifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail`

At that time "override init(frame: CGRect)" is called. and no value is assigned to shopCategory at the time of init that's the reason you are getting nil.

Add a function "getShopCategory" into MenuBarItemDetail and when every you want to access the value of shopCategory you can get that using getShopCategory function.

import Foundation
import UIKit

    class MenuBarItemDetail: UICollectionViewCell  {
        var shopCategory : String?

        func  getShopCategory()  {
         print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
        }


    }

Controller Class

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail
        cell.shopCategory = "600"
        print(cell.shopCategory)
        cell.getShopCategory()
        return cell
    }

cell is the current instance of MenuBarItemDetail so it will return assigned value of shopCategory

Please let me know if it's working for you. Thank you

Abhishek Sharma
  • 475
  • 3
  • 13