gpt4 book ai didi

ios - UICollectionViewCell 没有选择项目不工作

转载 作者:行者123 更新时间:2023-12-05 06:56:46 25 4
gpt4 key购买 nike

我对 TableVieCell 中的 collectionView 有疑问。当我点击 collectionCell 时,didSelectItemAt 不会被调用。我在 collectionCell 中有一个按钮,所以我尝试禁用用户交互并启用 contentView userInteraction,但它没有用。

My Table cell

红色矩形是tableCell,蓝色矩形是table view cell中的collecionView

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


if let cell = tableView.dequeueReusableCell(withIdentifier: "MedCellWithCollection") as? MedCellWithCollection{

let backgroundView = UIView()
backgroundView.backgroundColor = UIColor.white.withAlphaComponent(0.0)
cell.selectedBackgroundView = backgroundView

cell.setMedName(name: self.medCatalog[indexPath.row].nombre, uso: self.medCatalog[indexPath.row].uso , array: self.medCatalog[indexPath.row].enfermedades[0].aplicaciones , index: indexPath.row)

cell.layoutIfNeeded()
cell.layoutSubviews()
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()


return cell
}

}

return UITableViewCell()
}

表格单元格

class MedCellWithCollection: UITableViewCell {

//Outlets
@IBOutlet weak var medText: UILabel!
@IBOutlet weak var uso: UILabel!
@IBOutlet weak var arrowIcon: UIImageView!

@IBOutlet weak var CollectionView: UICollectionView!

//Variables
var dosesType:[Aplicacion]?


override func awakeFromNib() {
super.awakeFromNib()
self.collectionViewSetUp()
// Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}


func setMedName(name: String, uso: String, array: [Aplicacion], index: Int){
self.medText.text = name
self.uso.text = uso

self.dosesType = array

self.CollectionView.reloadData()
}


}
extension MedCellWithCollection: UICollectionViewDataSource, UICollectionViewDelegate{

func collectionViewSetUp(){
self.CollectionView.delegate = self
self.CollectionView.dataSource = self
}


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.dosesType?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "doseColletion", for: indexPath as IndexPath) as? DoseCollection {


cell.setButtonConfig(doseType: self.dosesType![indexPath.row].metodo , index: indexPath.row)

return cell
}

return UICollectionViewCell()

}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("tapped")
}

}

集合单元格

import UIKit

class DoseCollection: UICollectionViewCell {

//Outlets
@IBOutlet weak var Button: Button!

//Variables
let constants = Constants()

func setButtonConfig(doseType: String, index: Int){

self.Button.titleLabel?.text = doseType

self.Button.backgroundColor = constants.COLOR_ARRAY[index]


}


override func layoutSubviews() {
super.layoutSubviews()
}

override func layoutIfNeeded() {
super.layoutIfNeeded()
}

}

最佳答案

可能的解决方案:

1) 应该是 tableView selection 属性的单选,可以通过 tableView.allowsSelection = true

以编程方式完成

table view Selection property

2) 该类不是该 TableView 的 UITableViewDelegate,尽管 UITableViewController 应该自动设置它。

tableView?.delegate = self

3) 如果 UITapGestureRecognizer 出现问题:

let tap = UITapGestureRecognizer(target: self, action:Selector("dismissKeyboard"))
view.addGestureRecognizer(tap)
tap.cancelsTouchesInView = false

BOL :)

关于ios - UICollectionViewCell 没有选择项目不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65068361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com