单元格变量为零,其中设置了值 collectionview 索引路径
cell variable is nil where value was set collectionview index path
我花了很多时间..但我不明白为什么它不起作用..调用索引路径的单元格并正确设置值但是单元格class我发现零值..如果你需要任何信息然后让我知道。
在我的 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
。
Solution
自定义单元类中的变量
var myVar : String?
在您的自定义单元类中创建一个方法
func myMethod(str : String){
myVar = str
print("var : \(myVar)")
}
在cellForItem
中,像这样调用你的函数
cell.myMethod(str: "343")
Output
写的时候
let cell = collectionView.dequeueReusableCell(withReuseId`entifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail`
那时"override init(frame: CGRect)"被调用。
并且在初始化时没有为 shopCategory 分配任何值,这就是你得到 nil 的原因。
在 MenuBarItemDetail 中添加一个函数 "getShopCategory",当您每次想要访问 shopCategory 的值时,您可以使用 getShopCategory 函数获取它。
import Foundation
import UIKit
class MenuBarItemDetail: UICollectionViewCell {
var shopCategory : String?
func getShopCategory() {
print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
}
}
控制器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 是 MenuBarItemDetail 的当前实例,因此它将 return 赋值为 shopCategory
请告诉我它是否适合您。
谢谢
我花了很多时间..但我不明白为什么它不起作用..调用索引路径的单元格并正确设置值但是单元格class我发现零值..如果你需要任何信息然后让我知道。
在我的 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
。
Solution
自定义单元类中的变量
var myVar : String?
在您的自定义单元类中创建一个方法
func myMethod(str : String){ myVar = str print("var : \(myVar)") }
在
cellForItem
中,像这样调用你的函数cell.myMethod(str: "343")
Output
写的时候
let cell = collectionView.dequeueReusableCell(withReuseId`entifier: "MenuBarItemDetail", for: indexPath) as! MenuBarItemDetail`
那时"override init(frame: CGRect)"被调用。 并且在初始化时没有为 shopCategory 分配任何值,这就是你得到 nil 的原因。
在 MenuBarItemDetail 中添加一个函数 "getShopCategory",当您每次想要访问 shopCategory 的值时,您可以使用 getShopCategory 函数获取它。
import Foundation
import UIKit
class MenuBarItemDetail: UICollectionViewCell {
var shopCategory : String?
func getShopCategory() {
print("shopCategory :-> ...i am calling from cell :\(shopCategory)")
}
}
控制器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 是 MenuBarItemDetail 的当前实例,因此它将 return 赋值为 shopCategory
请告诉我它是否适合您。 谢谢