Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 单元格变量为nil,其中值设置为collectionview索引路径_Ios_Swift_Swift2 - Fatal编程技术网

Ios 单元格变量为nil,其中值设置为collectionview索引路径

Ios 单元格变量为nil,其中值设置为collectionview索引路径,ios,swift,swift2,Ios,Swift,Swift2,我花了很多时间。。但是我不明白为什么它不起作用。。已调用索引路径的单元格并正确设置值,但我发现单元格类的值为nil。。如果您需要任何信息,请告诉我 在我的collectionview索引路径中 在collectionview单元中 这是我的密码: 对于collectionview: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollec

我花了很多时间。。但是我不明白为什么它不起作用。。已调用索引路径的单元格并正确设置值,但我发现单元格类的值为nil。。如果您需要任何信息,请告诉我

在我的collectionview索引路径中

在collectionview单元中

这是我的密码:

对于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
    }
对于单元:

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

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

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

因为您的
awakeFromNib
方法首先调用,然后调用
cellForRow
。您在
cellForRow
中分配变量的值,所以当第一个项目执行时,其值为
nil

解决方案

  • 自定义cellclass中的变量

    var myVar : String?
    
    func myMethod(str : String){
        myVar = str
        print("var : \(myVar)")        
    }
    
  • 在自定义cellclass中创建一个方法

    var myVar : String?
    
    func myMethod(str : String){
        myVar = str
        print("var : \(myVar)")        
    }
    
  • cellForItem
    中,如下调用函数

    cell.myMethod(str: "343")
    
  • 输出

    当你写作时

    let cell = collectionView.dequeueReusableCell(withReuseId`entifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail`
    
    此时调用“overrideinit(frame:CGRect)”。 并且在初始化时没有为shopCategory分配值,这就是您得到零的原因

    将函数“getShopCategory”添加到MenuBarItemDetail中,每次需要访问shopCategory的值时,都可以使用getShopCategory函数获取该值

    import Foundation
    import UIKit
    
        class MenuBarItemDetail: UICollectionViewCell  {
            var shopCategory : String?
    
            func  getShopCategory()  {
             print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
            }
    
    
        }
    
    控制器类

     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
        }
    
    单元格是MenuBarItemDetail的当前实例,因此它将返回shopCategory的赋值

    请让我知道它是否适合你。
    谢谢

    这是可选值使用打印(cell.shopCategory)!对我已经做了,请检查第一张图片,但没有结果。。你是那个意思吗one@Lalitkumar是可选值将其用作我的第一个picture@cristanlika首先在单元类UITableViewDataSource、UITableViewDelegate中不需要这些,因为这是集合视图单元,所以您需要将所有委托放入视图控制器并删除init函数。不需要。您可以使用awakeFromNib首先,我在项目中的任何地方都没有使用awakeFromNib;其次,如果是这样的话,解决方案是什么。你能告诉我该怎么做吗?Init方法也叫first,看看吧。是的。我能做什么?解决办法是什么please@cristanlika用我得到的输出检查解决方案。接受答案,如果对你有效,就投票表决,否则就写下你的问题。