尝试使用没有ViewController的CollectionViewController时出错

时间:2015-09-27 01:31:58

标签: ios swift uicollectionview swift2

我整天都在尝试使用CollectionViewController而不是ViewController。在我的故事板中,我有一个CollectionViewController,里面的集合视图,以及单元格内的一个按钮。 我的代码是: 在CollectionViewController上:

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return numberOfButtons
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! CustomCollectionViewCell

    cell.buttonInCell.selected = buttonsStatusList[indexPath.row]
    cell.setContents(indexPath.row)
    cell.updateButtonAppearance()

    return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    let tappedCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell
    tappedCell.buttonInCell.selected = !tappedCell.buttonInCell.selected
    buttonsStatusList[indexPath.row] = tappedCell.buttonInCell.selected
    tappedCell.updateButtonAppearance()
}

在CustomCollectionViewCell中我有:

@IBOutlet weak var buttonInCell: UIButton!

let buttonBorderWidth: CGFloat = 1.0
var buttonRadius: CGFloat!

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    buttonRadius = frame.height / 2
}

func setContents(row: Int) {
    buttonInCell.setTitle(String(row + 1), forState: .Normal)
    buttonInCell.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
    buttonInCell.layer.cornerRadius = buttonRadius
    buttonInCell.layer.borderColor = UIColor.blueColor().CGColor
    buttonInCell.layer.borderWidth = buttonBorderWidth
}

func updateButtonAppearance() {
    if buttonInCell.selected {
        buttonInCell.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        buttonInCell.layer.backgroundColor = UIColor.blueColor().CGColor
    } else {
        buttonInCell.setTitleColor(UIColor.blueColor(), forState: .Normal)
        buttonInCell.layer.backgroundColor = UIColor.whiteColor().CGColor
    }
}

我猜问题是在故事板的连接中,但我找不到它。 我得到的错误是: Could not cast value of type 'UICollectionViewCell' (0x10c7b6408) to 'UICollectionViewController.CustomCollectionViewCell' (0x10aee8470).dequeueReusableCellWithReuseIdentifier

1 个答案:

答案 0 :(得分:0)

那么有很多问题导致这种情况发生,尤其是在CustomCell中定义你的东西。

  • 我在cellForItemAtIndexPath
  • 中重新定义了单元格类
  • addTarget点击方法updateButtonAppearance()的每个单元格中的句柄按钮:

    cell.buttonInCell.addTarget(self, action: "updateButtonAppearance:", forControlEvents: UIControlEvents.TouchUpInside)

  • 使用buttonStatusList内的cellForItemAtIndexPath布尔数组跟踪所选项目。

更新:要取消选择该项并将颜色返回默认值,请将updateButtonAppearance中的if语句替换为:

if cell.buttonInCell.backgroundColor == UIColor.blueColor() {
    cell.buttonInCell.setTitleColor(UIColor.blueColor(), forState: .Normal)
    cell.buttonInCell.layer.backgroundColor = UIColor.whiteColor().CGColor
    buttonsStatusList[sender.tag] = false
} else {
    cell.buttonInCell.setTitleColor(UIColor.whiteColor(), forState: .Normal)
    cell.buttonInCell.layer.backgroundColor = UIColor.blueColor().CGColor
    buttonsStatusList[sender.tag] = true

}

注意:您不再需要使用didSelectItemAtIndexPath方法了!