无法读取数据,因为它的格式不正确.如何解决初始化 Swift 类错误?



>我有单元格类:

// CELL
class CollectionCell: ScalingCarouselCell {

@IBOutlet weak var title: MLabelWhite!
@IBOutlet weak var name: MLabelWhite!
@IBOutlet weak var date: MLabelLight!
@IBOutlet weak var views: MLabelLight!
var id: String = ""
override init(frame: CGRect) {
super.init(frame: frame)
title.translatesAutoresizingMaskIntoConstraints = false
name.translatesAutoresizingMaskIntoConstraints = false
date.translatesAutoresizingMaskIntoConstraints = false
views.translatesAutoresizingMaskIntoConstraints = false

mainView = UIView(frame: contentView.bounds)
contentView.addSubview(mainView)
mainView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
mainView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
mainView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
mainView.topAnchor.constraint(equalTo: contentView.topAnchor),
mainView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
])
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}

我有每个单元格带有 ID 的集合视图。当我按下项目ID应该打印... 问题是 100% 与init

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

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCollection", for: indexPath)
if let scalingCell = cell as? CollectionCell {
// HERE 
// idList is ok. The problem is scalingCell.id is NOT SAVED
scalingCell.id = idList[indexPath.row]
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCollection", for: indexPath)
if let scalingCell = cell as? CollectionCell {
print(scalingCell.id)
}
}

无法读取数据,因为它的格式不正确。

问题是将状态SAVEID到选定的CELL中。

如果有人知道如何解决这个问题,请发布答案。

当前的解决方案不是将状态保存到cell.id中。 只需直接将idList与选定的单元格行 ID 一起使用:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellCollection", for: indexPath)
if cell is CollectionCell {
print(idList[indexPath.row])
}
}

相关内容

最新更新