【发布时间】:2018-11-27 11:50:57
【问题描述】:
我有这段代码,其中我将“ColorCell”类留给了我的单元格,之后我想点击我的收藏品的一个按钮,我创建了 ul 层(只有选定的层)以获得一个想法并知道哪个premo。代码工作,在函数内调试但不执行它,它只运行tapColor
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ColorCell.cellId, for: indexPath) as! ColorCell
if cell.isSelected {
cell.locationImage.layer.borderWidth = 1
cell.locationImage.layer.borderColor = UIColor.white.cgColor
self.delegate?.tapColor(colorRound: datasource[indexPath.item].color)
}
}
class ColorCell: UICollectionViewCell {
static let reuseIdentifier = "TextColorRoundCell_RID"
static let cellId = "ImageSliderCell__"
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}
var cellID: String?
var locationImage = UIImageView().then{
$0.contentMode = UIView.ContentMode.scaleAspectFit
$0.layer.cornerRadius = (((UIScreen.main.bounds.size.width/8)*0.8)/2)
// $0.layer.borderWidth = 1
// $0.layer.borderColor = UIColor.white.cgColor
}
func setupViews(){
self.addSubview(self.locationImage)
self.locationImage.activate([
locationImage.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.8),
locationImage.heightAnchor.constraint(equalTo: self.locationImage.widthAnchor),
locationImage.centerXAnchor.constraint(equalTo: self.centerXAnchor),
locationImage.centerYAnchor.constraint(equalTo: self.centerYAnchor),
])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
【问题讨论】:
-
"得到一个想法并知道哪个 premo。"你用过谷歌翻译吗?哈哈哈无论如何,如果您想为单元格内的按钮提供特定功能,您应该使用该按钮并向其添加一个目标,例如“button.addTarget(self, #selector(yourFunction:), sender: nil)” 现在当你点击一个单元格时,它会触发函数“didSelectItemAt”并且只有函数 tapColor 被调用
-
@FrancescoDestino 好的。那么我应该像使用 tapColor 一样创建一个函数吗?并简单地声明创建图层?但是,这种方式不会为所有按钮创建它吗?
-
我试图理解你的问题:你希望你的单元格内的按钮可以执行一个动作,对吧?!或者,也许您正在谈论将按钮作为您的单元格?
-
@FrancescoDestino ho una 10 di button all'interno della collection, che dichiaro con unico arrey, vorrei che quando premo su uno mi si crei un layer attorno。在 modo che so che ho selezionato quello
-
Quindi a quanto ho capito non vengono eseguiti queste due righe cell.locationImage.layer.borderWidth = 1 cell.locationImage.layer.borderColor = UIColor.white.cgColor Giusto?