gpt4 book ai didi

ios - 尝试在没有 ViewController 的情况下使用 CollectionViewController 时出错

转载 作者:行者123 更新时间:2023-11-30 14:02:37 25 4
gpt4 key购买 nike

我一整天都在尝试使用 CollectionViewController 而不是 ViewController。在我的 Storyboard中,我有一个 CollectionViewController、内部的 Collection View 和单元格内的按钮。我的代码是:在 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
}
}

我猜问题出在 Storyboard的连接上,但我找不到它。我得到的错误是:无法将“UICollectionViewCell”类型的值 (0x10c7b6408) 转换为“UICollectionViewController.CustomCollectionViewCell”(0x10aee8470)。dequeueReusableCellWithReuseIdentifier

最佳答案

嗯,有很多问题导致了这种情况的发生,特别是在 CustomCell 中定义您的内容。

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

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

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

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

关于ios - 尝试在没有 ViewController 的情况下使用 CollectionViewController 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32803707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com