我正在尝试创建一个函数,通过它我可以切换UICollectionViewCell的背景色,这取决于选择了哪个单元格和选择了什么颜色。我创建了这两个函数:
func selectedCell (at indexPath: IndexPath) {
let indexPathForFirstRow = indexPath
gamePad.selectItem(at: indexPathForFirstRow, animated: false, scrollPosition: UICollectionView.ScrollPosition(rawValue: 0))
} @IBAction func selectedColor(_ sender: UIButton) {
let cell = self.selectedCell(at: IndexPath)
cell.backgroundcolor = sender.currentTitleColor
}在第二个函数中,出现了一个问题,即我不能使用indexPath或IndexPath。
我该如何解决这个问题?
这就是我使用IndexPath时显示的错误
无法将“IndexPath.Type”类型的值转换为预期的参数类型“IndexPath”
这就是我使用indexPath时显示的错误
未解析标识符“IndexPath”的使用;您的意思是“IndexPath”吗?
发布于 2018-10-02 07:33:26
你需要写一份协议。您可以为您的响应实现我的代码。您必须在selectedColor中定义哪个索引路径。
//自定义单元格
protocol colorDelegate {
func changeColor(sender : CustomCell)
}
class CustomCell: UICollectionViewCell {
var delegate:colorDelegate?
@IBAction func selectionButtonTapped(_ sender: Any) {
if delegate != nil {
delegate?.changeColor(sender: self)
}
}
}//视图控制器
@objc func changeColor(sender: CustomCell) {
if let indexpath = tableView.indexPath(for: sender) {
let cell = tableView.cellForRow(at: indexpath) as! CustomCell
cell.backgroundcolor = .white
}
} https://stackoverflow.com/questions/52603675
复制相似问题