无法使用Swift 3修改Custom collectionView中的标签单元格

时间:2016-12-27 16:11:56

标签: swift uicollectionview

这是我通过另一个例子修改的代码,collectionView控制器工作正常,但是来到了修改collectionCell中标签的代码 " cell.myLabel.text = self.items [indexPath.item]" ,我收到错误信息:"致命错误:在打开一个Optional值时意外发现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)!")
    }
}

my custom collection cell with only a label to display

这里是" 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")
}
}

1 个答案:

答案 0 :(得分:1)

使用UINib实例注册MyCollectionViewCell而不是CollectionView课程。

应该是:

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

代替:

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