我有这段代码,其中将“ ColorCell”类留给我的单元格,之后我想单击收藏夹中的一个按钮,我创建了ullayes(仅选定的一个)以了解一个想法哪个节目预告片。代码有效,在函数中调试但不执行,它仅运行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")
}
}
答案 0 :(得分:0)
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)
}
for singleCell in collectionView.visibleCells {
if singleCell != cell {
singleCell.layer.borderWidth = 0
singleCell.layer.borderColor = UIColor.clear
}
}
collectionView.reloadData()
}
每个risolvere问题的问题,Prova cosi,ci sarebbero和sacco di modi
编辑2
class ColorCell: UICollectionViewCell {
//Qui dentro inserisci tutti i componenti della cella
//var label: UILabel
//var image: UIImage etc..
override var isSelected: Bool {
didSet{
if isSelected {
//QUI cambi i colori degli elementi
} else {
//QUI resetti i colori come erano all'inizio
}
}
}
}