为什么我的收藏视图单元格很难点击?

时间:2019-12-22 19:08:29

标签: swift uicollectionview uicollectionviewcell

我有一个CollectionViewController和多个单元格。但是,当我似乎点击这些单元格之一时,在didSelectItemAt读取之前,我需要多次点击。我的细胞被均匀地隔开,至少大到足以被其轻拍。我只能在真实设备上而不是模拟器上遇到此问题。

这是我的代码

class TimeAvailabilityCell:UICollectionViewCell{

private let container:UIView = {
    let view = UIView()
    view.isUserInteractionEnabled = false
    view.translatesAutoresizingMaskIntoConstraints = false
    view.backgroundColor = .white
    view.layer.borderWidth = 1
    view.layer.borderColor = UIColor.systemBlue.cgColor
    view.layer.cornerRadius = 8
    return view
}()

public let timeLabel:UILabel = {
    let label = UILabel()
    label.isUserInteractionEnabled = false
    label.translatesAutoresizingMaskIntoConstraints = false
    label.numberOfLines = 0
    label.font = UIFont(name: "AppleSDGothicNeo-Regular", size: 18)
    label.textColor = .systemBlue
    return label
}()

override init(frame: CGRect) {
    super.init(frame:frame)

    self.backgroundColor = .green

    self.addSubview(container)
    container.widthAnchor.constraint(equalToConstant: 100).isActive = true
    container.heightAnchor.constraint(equalToConstant: 50).isActive = true
    container.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    container.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

    container.addSubview(timeLabel)
    timeLabel.centerXAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
    timeLabel.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}

这是我的CollectionViewController

class TimeSelectServiceController: UICollectionViewController, UICollectionViewDelegateFlowLayout{

var timeAvailableArray = [String]()
var timeTitle = ""

override func viewDidLoad() {
    super.viewDidLoad()
    view.backgroundColor = UIColor(red: 245/255, green: 245/255, blue: 245/255, alpha: 1)

    setupCollectionView()
}

override func viewDidLayoutSubviews() {
    self.updateCollectionView()
}

func setupCollectionView(){

    let inset = UIEdgeInsets(top: 0, left: 25, bottom: 80, right: 25)
    collectionView.contentInset = inset

    self.edgesForExtendedLayout = UIRectEdge.bottom
    collectionView.backgroundColor = .white
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.register(TimeAvailabilityCell.self, forCellWithReuseIdentifier: "cell")
    collectionView.register(SectionTitleHeader.self, forSupplementaryViewOfKind:UICollectionView.elementKindSectionHeader, withReuseIdentifier: "SectionTitleHeader")

    collectionView.allowsSelection = true
}

override func numberOfSections(in collectionView: UICollectionView) -> Int {
    1
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    timeAvailableArray.count
}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TimeAvailabilityCell
    let model = timeAvailableArray[indexPath.row]
    cell.timeLabel.text = model.description
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: self.collectionView.frame.width, height: 50)
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    let reusableview = UICollectionReusableView()
    switch kind {
    case UICollectionView.elementKindSectionHeader:
        //FOR HEADER VIEWS
        let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "SectionTitleHeader", for: indexPath) as! SectionTitleHeader

        headerView.title.text = timeTitle
        headerView.seeAll.isHidden = true
        headerView.backgroundColor = .white
        return headerView
    default:
        return reusableview
    }

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    if(section == 0){
        return CGSize(width: view.frame.width, height: 100)
    }
    return CGSize(width: view.frame.width, height: 100)
}

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let model = timeAvailableArray[indexPath.row]

    BookServiceDetails.details.serviceDate = timeTitle + " at " + model.description

    let viewController = ConfirmAppointmentController()
    self.navigationController?.pushViewController(viewController, animated: true)

    collectionView.deselectItem(at: indexPath, animated: true)
}




func updateCollectionView () {
    DispatchQueue.main.async(execute: {
        self.collectionView.reloadData()
    })
}

}

  

编辑

似乎我越难找到一个单元格。

2 个答案:

答案 0 :(得分:1)

TimeAvailabilityCell中将子视图添加到contentView而不是self中。 在子视图上禁用用户交互应该没有关系。

documentation: “要配置单元格的外观,请在contentView 属性中向视图添加将数据项的内容作为子视图呈现所需的视图。不要将子视图直接添加到单元格本身。”

override init(frame: CGRect) {
    super.init(frame:frame)

    self.backgroundColor = .green

    contentView.addSubview(container)
    container.widthAnchor.constraint(equalToConstant: 100).isActive = true
    container.heightAnchor.constraint(equalToConstant: 50).isActive = true
    container.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
    container.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true

    container.addSubview(timeLabel)
    timeLabel.centerXAnchor.constraint(equalTo: container.centerXAnchor).isActive = true
    timeLabel.centerYAnchor.constraint(equalTo: container.centerYAnchor).isActive = true
}

此:

    override func viewDidLayoutSubviews() {
        self.updateCollectionView()
    }

    func updateCollectionView () {
        DispatchQueue.main.async(execute: {
            self.collectionView.reloadData()
        })
    }

创建一个无限循环,在集合视图上调用reloadData,触发viewDidLayoutSubviews。基本上,收集视图会不断重新加载单元格,这会导致选择单元格工作不正常。

以下博客是有关如何设置和使用收藏夹视图的良好参考;使用情节提要或以编程方式。它应该在如何设置和配置收藏夹视图方面提供更多见解。

答案 1 :(得分:0)

我不知道这与它有什么关系,但是我删除了collectionView.dataSource = self,现在一切正常。