【问题标题】:Error when trying to use a CollectionViewController without a ViewController尝试使用没有 ViewController 的 CollectionViewController 时出错
【发布时间】:2015-09-27 01:31:57
【问题描述】:

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

【问题讨论】:

  • 你在storyboard中设置单元格的类名了吗?
  • 是的,@YiminLin,实际上,我也尝试删除有关 CustomCell 的所有内容并仅使用 UICollectionViewCell 但我得到一个没有单元格的空白屏幕(我在情节提要中将单元格的背景设置为蓝色)
  • 是的,@AaoIi。问题已经解决了!我仍然不知道为什么didSelectItemAtIndexPath 不起作用,所以我可以取消选择按钮,但你做了你的一部分。非常感谢!
  • 你没有改变太多。我是编程新手,所以我把函数放在了它们可能不应该放在的地方。
  • 欢迎您,我也会努力解决的!

标签: ios swift uicollectionview swift2


【解决方案1】:

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

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

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

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

更新:要取消选择项目并将颜色恢复为默认值,请将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 方法了!

【讨论】:

    猜你喜欢
    • 2018-10-22
    • 1970-01-01
    • 2021-12-27
    • 2017-07-18
    • 2017-02-08
    • 2018-12-02
    • 2020-09-07
    • 2021-09-14
    • 2019-04-09
    相关资源
    最近更新 更多