ios - Collection View 不滚动之一,容器 View 内

标签 ios swift scroll uicollectionview

所以我在父 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.
    }

}

最佳答案

您的 View 类被标记为 TimeCollectionViewCell 而不是 UIView

enter image description here

实现委托(delegate)方法,它就会起作用。

关于ios - Collection View 不滚动之一,容器 View 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42299514/

相关文章:

ios - 关于通过应用程序加载器向 iTunes Connect 提交我的 iOS 应用程序的更新

swift - 上传到 App Store 时,Cocoa Pods 会重新安装吗?

ios - 如何从自定义 View 单元格重新加载数据?

ios - 如何在iOS 5中实现字母滚动?

ios - 无法创建临时目录

iOS越狱-如何在后台获取位置

Android标签点击滚动到顶部

ios - 绘图不会随 drawRect 中的文本滚动

ios - 在后台模式下从 Alamofire 获取数据

swift - MKUserTrackingButton - 如何在不缩小的情况下获取用户位置的中心 map ?