实在不知道怎么让collection View Cell(didSelect)函数正常工作
I really don't know how to make collectionViewCell (didSelected) function work properly
我在 Swift 5 中制作日历,现在我遇到了问题。
首先这是我的代码:
import UIKit
class YearViewController: UIViewController {
@IBOutlet weak var yearName: UILabel!
@IBOutlet weak var yearCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
yearName.text = String(CalenderBrain.init().curruntYear)
yearCollectionView.dataSource = self
yearCollectionView.delegate = self
yearCollectionView.register(UINib(nibName: "ReusableYearCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell3")
if let layout = self.yearCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
layout.minimumInteritemSpacing = -10
layout.minimumLineSpacing = 1
}
}
}
extension YearViewController : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell3", for: indexPath) as! ReusableYearCollectionViewCell
cell.monthName.text = Calendar.current.monthSymbols[indexPath.row]
if cell.monthName.text == CalenderBrain.init().curruntMonthName(){
cell.monthName.textColor = .red
cell.tag = 999
}
cell.month = indexPath.row + 1
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return .init(width: 126.3, height:(570-3)/4)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "lastOne") as? ViewController
vc?.whatIGetFromYearViewController = indexPath.row + 1
vc?.curruntMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row]
vc?.nextMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row + 1]
self.navigationController?.pushViewController(vc!, animated: true)
print("i'm pressed")
}
}
我想如果我的单元格被点击然后会发生 segue。
这是我手机的图片。 (我在 collectionViewCell 中创建了 collectionView。)
问题是仅当我单击标签的左侧部分时才会出现 segue。如果我在 CollectionViewCell 中单击 collectionView,则不会发生 segue。我认为那是因为 Swift 将 collectionViewCell 中的 collectionView 识别为另一个 CollectionView。所以现在,我想让 collectionViewCell 被点击并发生 segue,而不管我点击的部分是什么。这可能吗?
尝试在 collectionViewCell 中设置 collectionView.isUserInteractionEnable = false
。
此代码将阻止用户对 collectionViewCell 中的 collectionView 进行操作,仅接收主 collectionView 的 didSelectItem 操作。
我在 Swift 5 中制作日历,现在我遇到了问题。 首先这是我的代码:
import UIKit
class YearViewController: UIViewController {
@IBOutlet weak var yearName: UILabel!
@IBOutlet weak var yearCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
yearName.text = String(CalenderBrain.init().curruntYear)
yearCollectionView.dataSource = self
yearCollectionView.delegate = self
yearCollectionView.register(UINib(nibName: "ReusableYearCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "MyCell3")
if let layout = self.yearCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
layout.minimumInteritemSpacing = -10
layout.minimumLineSpacing = 1
}
}
}
extension YearViewController : UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 12
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell3", for: indexPath) as! ReusableYearCollectionViewCell
cell.monthName.text = Calendar.current.monthSymbols[indexPath.row]
if cell.monthName.text == CalenderBrain.init().curruntMonthName(){
cell.monthName.textColor = .red
cell.tag = 999
}
cell.month = indexPath.row + 1
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return .init(width: 126.3, height:(570-3)/4)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(withIdentifier: "lastOne") as? ViewController
vc?.whatIGetFromYearViewController = indexPath.row + 1
vc?.curruntMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row]
vc?.nextMonthNameThatIHaveToPut = Calendar.current.monthSymbols[indexPath.row + 1]
self.navigationController?.pushViewController(vc!, animated: true)
print("i'm pressed")
}
}
我想如果我的单元格被点击然后会发生 segue。
这是我手机的图片。 (我在 collectionViewCell 中创建了 collectionView。)
问题是仅当我单击标签的左侧部分时才会出现 segue。如果我在 CollectionViewCell 中单击 collectionView,则不会发生 segue。我认为那是因为 Swift 将 collectionViewCell 中的 collectionView 识别为另一个 CollectionView。所以现在,我想让 collectionViewCell 被点击并发生 segue,而不管我点击的部分是什么。这可能吗?
尝试在 collectionViewCell 中设置 collectionView.isUserInteractionEnable = false
。
此代码将阻止用户对 collectionViewCell 中的 collectionView 进行操作,仅接收主 collectionView 的 didSelectItem 操作。