我试图在UIViewController中使用2个不同的collectionview
问题是,当我按下第一个collectionView
时,调用会进入didselectItem,但是当我按下另一个collectionviewcell时,它不会被调用。
以下是我的代码: -
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == participantsCollectionView {
print("participant")
} else {
print("image")
}
}
布局设置
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
layout.itemSize = CGSize(width: 72, height: 72)
layout.scrollDirection = .vertical
let frame = CGRect(x: 24, y: 766, width: 326, height: 156)
eventImagesCollectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
eventImagesCollectionView.dataSource = self
eventImagesCollectionView.delegate = self
let imagesNib = UINib(nibName: "EventImagesCollectionViewCell", bundle: nil)
eventImagesCollectionView.register(imagesNib, forCellWithReuseIdentifier: "cell")
eventImagesCollectionView.backgroundColor = UIColor.clear
self.contentView.addSubview(eventImagesCollectionView)
self.contentView.removeConstraints(self.stackView.constraints)
eventImagesCollectionView <- [
Top(24).to(self.stackView),
Left(24),
Right(24),
Width(326),
Height(156),
]
eventImagesCollectionView.isUserInteractionEnabled = true
第二个集合视图
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
layout.itemSize = CGSize(width: 36, height: 36)
layout.scrollDirection = .horizontal
let frame = CGRect(x: 0, y: 433, width: 352, height: 36)
participantsCollectionView = UICollectionView(frame: frame, collectionViewLayout: layout)
participantsCollectionView.dataSource = self
participantsCollectionView.delegate = self
let nib = UINib(nibName: "EventParticipantsCollectionViewCell", bundle: nil)
participantsCollectionView.register(nib, forCellWithReuseIdentifier: "cell")
participantsCollectionView.backgroundColor = UIColor.clear
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(participantsCollectionView)
self.eventDescriptionLabel <- [
Top(12).to(self.eventTitleLabel)
]
participantsCollectionView <- [
Top(12).to(self.eventDescriptionLabel),
Left(0),
Right(24),
Width(351),
Height(36),
]
participantsCollectionView.easy_reload()
let goingButtonFrame = CGRect(x: 103, y: 485, width: 88, height: 29)
let goingButton = UIButton(frame: goingButtonFrame)
var colors = [UIColor(hex: "F65557"), UIColor(hex: "FA8C35")]
goingButton.backgroundColor = UIColor(gradientStyle: .leftToRight, withFrame: goingButton.frame, andColors: colors)
goingButton.tintColor = .white
goingButton.setTitle("Going", for: .normal)
let verifyImage = UIImage(named: "ic_done_black_24px")
goingButton.titleLabel?.font = UIFont(name: "Brandon_reg", size: 15)
goingButton.cornerRadius = 4
goingButton.setImage(verifyImage, for: .normal)
self.contentView.addSubview(goingButton)
goingButton <- [
Top(12).to(participantsCollectionView),
Width(88),
Height(29),
Left(103)
]
let inviteButtonFrame = CGRect(x: 203, y: 485, width: 70, height: 29)
let inviteButton = UIButton(frame: inviteButtonFrame)
inviteButton.borderColor = UIColor(hex: "FF6F5F")
inviteButton.borderWidth = 1
inviteButton.backgroundColor = .clear
inviteButton.setTitleColor(UIColor(hex: "FF6F5F"), for: .normal)
inviteButton.setTitle("Invite", for: .normal)
inviteButton.titleLabel?.font = UIFont(name: "Brandon_reg", size: 15)
inviteButton.cornerRadius = 4
inviteButton.setImage(verifyImage, for: .normal)
self.contentView.addSubview(inviteButton)
inviteButton <- [
Top(12).to(participantsCollectionView),
Width(70),
Height(29),
Right(102),
Left(12).to(goingButton)
]
self.stackView <- [
Top(12).to(goingButton),
Left(0),
Right(0)
]