【问题标题】:Cannot modify label cell in Custom collectionView with Swift 3无法使用 Swift 3 修改自定义 collectionView 中的标签单元格
【发布时间】:2016-12-27 16:11:55
【问题描述】:

这是我通过另一个示例修改的代码,collectionView 控制器工作正常,但是来修改collectionCell 中标签的代码 “cell.myLabel.text = self.items[indexPath.item]” ,我得到错误信息:“致命错误:在展开可选值时意外发现 nil” 程序有什么问题?

这里是集合视图控制器代码:

import UIKit


class myViewController: UIViewController, UICollectionViewDataSource ,UICollectionViewDelegateFlowLayout, UICollectionViewDelegate {

    let leftAndRightPaddings: CGFloat = 80.0
    let numberOfItemsPerRow: CGFloat = 7.0
    let screenSize: CGRect = UIScreen.main.bounds
    private let cellReuseIdentifier = "collectionCell"

override func viewDidLoad() {
    super.viewDidLoad()
    let flowLayout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout)
    collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.backgroundColor = UIColor.cyan

    self.view.addSubview(collectionView)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


let reuseIdentifier = "collectionCell" // also enter this string as the cell identifier in the storyboard
var items = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"]


// MARK: - UICollectionViewDataSource protocol

// tell the collection view how many cells to make
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.items.count
}

// make a cell for each cell index path
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    // get a reference to our storyboard cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath ) as! MyCollectionViewCell

    let k = indexPath.item
    print("item="+items[k])

    //cell.myLabel.text = "a"
    //cell.backgroundColor = UIColor.brown
    // Use the outlet in our custom class to get a reference to the UILabel in the cell
    cell.myLabel.text =  self.items[indexPath.item]
    //cell.backgroundColor = UIColor.cyan // make cell more visible in our example project

    return cell
}

// MARK: - UICollectionViewDelegate protocol

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        // handle tap events
        print("You selected cell #\(indexPath.item)!")
    }
}

这里是“collectionCell”代码:

import UIKit

class MyCollectionViewCell: UICollectionViewCell {

@IBOutlet weak var myLabel: UILabel!

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

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

【问题讨论】:

  • 调试你的代码 - nil 是什么可选的,为什么!? myLabel 没有连接吗?
  • 已连接!确认!
  • 继续我提到的第一件事。
  • 从您的 MyCollectionViewCell 中删除 init 方法

标签: swift uicollectionview


【解决方案1】:

使用您的 CollectionView 实例注册 UINib 而不是 MyCollectionViewCell 类。

应该是:

collectionView.register(UINib(nibName: "View2", bundle: nil), forCellWithReuseIdentifier: cellReuseIdentifier)

代替:

collectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: cellReuseIdentifier)

【讨论】:

  • 并删除 "fatalError("init(coder:) has not been implemented")" ,替换为 super.init(coder: aDecoder)
【解决方案2】:

这里是 SWIFT 5 的代码,在 CollectionViewController 中有 2 个单元格

import UIKit

// this is your UILabel
@IBOutlet weak var labelCell: UILabel!

  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MatrixCollectionViewCell

        switch indexPath.row {
        case 0:
            cell.labelCell.text = " "
        case 1:
            cell.labelCell.text = " "
        default:
            break
        }

// make extention for your CollectionViewController


extension CollectionViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let collectionCell = collectionView.frame.width / 2 - 20
        
        return CGSize(width: collectionCell, height: collectionCell)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多