CollectionViewController

CollectionViewController

我整天都在尝试使用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

最佳答案

好吧,有很多问题导致这种情况的发生,特别是通过在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方法!

07-28 01:52