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

标签 ios swift uicollectionview swift2

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

相关文章:

ios - API 请求仅在我传递参数时超时

swift - 具有线性渐变的饼图 - swift

ios - 转换[字节]?到[字节]

ios - 如何使用 UICollectionViewController 设置具有多个部分的 1 标题

swift - 仅在获取用户位置后获取 Firebase/Geofire 数据

ios - 在 UICollectionView 中滚动 2-3 次后单元格消失

ios - 带有django csrf的ionic/angularjs可以在本地服务器上工作,但不能在设备上工作

ios - 将数据从自定义单元格(在 ViewController 上的表中)传递到 uiviewcontroller 本身 Swift 3 iOS

ios - 当另一个警报出现时,alertview 关闭

ios - 用 Throttle 替换所有 UIButton 触摸事件