所以我在父视图中有三个containerViews,第一个包含静态tableview,工作正常。 第二个ContainerView里面有一个collectionView,它可以横向滚动,并且可以选择& didDeselect也被调用了。 第三个containerView还有另一个collectionView;但是,它不会在storyboard中设置水平滚动,也不会调用didSelect和didDeselect。
private let reuseIdentifier = "TimeCollectionViewCell"
class TimeCollectionViewDataSource: NSObject, UICollectionViewDataSource {
private let dataSource = TimeModel()
private let dateFormatter = DateFormatter()
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataSource.getReservationTimeIntervals().count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! TimeCollectionViewCell
// dateFormatter indicates how the string will look like
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
// get the array of times
let times = dataSource.getReservationTimeIntervals()
cell.timeLabel.text = dateFormatter.string(from: times[indexPath.row])
return cell
}
}
import UIKit
class DateCollectionVC: UIViewController, UICollectionViewDelegate {
@IBOutlet weak var dateCollectionView: UICollectionView!
@IBOutlet weak var monthLabel: UILabel!
private var dateFormatter = DateFormatter()
private let currentDate = NSDate()
private var daysInMonth = Int()
private let dataSource = DateCollectionViewDataSource()
private var isSelected = false
override func viewDidLoad() {
super.viewDidLoad()
dateCollectionView.dataSource = dataSource
dateCollectionView.delegate = self
monthLabel.text = getMonth()
}
// based on current Date, just extract the Month
func getMonth() -> String {
dateFormatter.dateFormat = "MMMM"
return dateFormatter.string(from: currentDate as Date)
}
// user taps cell & check Mark appreas or disappears
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = collectionView.cellForItem(at: indexPath) as! DateCollectionViewCell
// checks to see if same cell was tapped again
if !isSelected {
selectedCell.checkMarkImageView.alpha = 0.75
isSelected = true
} else {
selectedCell.checkMarkImageView.alpha = 0
}
}
// user taps a different cell and check mark disappears
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let selectedCell = collectionView.cellForItem(at: indexPath) as! DateCollectionViewCell
selectedCell.checkMarkImageView.alpha = 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}