所以我在父 View 中有三个容器 View ,第一个包含一个工作正常的静态 TableView 。 第二个 ContainerView 内部有一个 collectionView,它可以水平滚动,并且 didSelect 和 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.
}
}
最佳答案
关于ios - Collection View 不滚动之一,容器 View 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299514/