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

问题描述 投票:0回答:2

我有一个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()
    })
}

}

编辑

似乎我的单元格越多,单击它越难。

swift uicollectionview uicollectionviewcell
2个回答
0
投票

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。基本上,收集视图会不断重新加载单元格,这会导致选择单元格工作不佳。

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.