在展开CellViewWithTag UICollectionView时找到nil

时间:2015-08-06 10:48:19

标签: ios tags uicollectionview uicollectionviewcell

我使用过UITableViews,所以我看不出有什么区别。

所以我的故事板设置如下:

enter image description here

使用标识符

enter image description here

标签设置如下

enter image description here

enter image description here

我的代码是这样的:

类MoreController:UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    @IBOutlet var moreCollection: UICollectionView!

    var moreitems = [  [ MoreItem(title: "Maps", imageUrl: "Map Marker-25.png"), MoreItem(title: "Dart", imageUrl: "Train-25.png"), MoreItem(title: "Feedback", imageUrl: "Feedback-25.png"), MoreItem(title: "Help", imageUrl: "High Priority-25.png")  ]  ]

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor(red: 52/255, green: 138/255, blue: 169/255, alpha: 1.0)

        moreCollection.delegate = self
        moreCollection.dataSource = self

    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return moreitems.count
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return moreitems[section].count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MoreCell", forIndexPath: indexPath) as! UICollectionViewCell

        let icLab = cell.viewWithTag(4001) as! UILabel
        let imView = cell.viewWithTag(4002) as! UIImageView

        icLab.text = moreitems[indexPath.section][indexPath.row].title
        imView.image = UIImage(named: moreitems[indexPath.section][indexPath.row].imageUrl)

        return cell
    }
}

我收到了错误fatal error: unexpectedly found nil while unwrapping an Optional value

let icLab = cell.viewWithTag(4001) as! UILabel

let imView = cell.viewWithTag(4002) as! UIImageView

知道为什么吗?

0 个答案:

没有答案