gpt4 book ai didi

Swift 如何取消选择 Collection Cell

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:52 24 4
gpt4 key购买 nike

TenViewController 代码:

class TenViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var selectedCell: UICollectionViewCell!
var arrayLocation = ["aaa", "bbb", "ccc", "ddd", "eee"]
var myCollectionView: UICollectionView!
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int
{
return arrayLocation.count
}

collection delegate

            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
cell.backgroundColor = UIColor.white
cell.titleLabel?.text = arrayLocation[indexPath.row]
cell.titleLabel.font = UIFont.systemFont(ofSize: 24)
return cell
}
// check cell and do somthing
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath)
{ selectedCell = myCollectionView.cellForItem(at: indexPath)! selectedCell.contentView.backgroundColor = UIColor.red
collectionView.allowsSelection = true
collectionView.allowsMultipleSelection = true
}

I trying to use this func but it doesn't work

            func collectionView(_ collectionView: UICollectionView,didDeselectItemAt indexPath: IndexPath) {
myCollectionView.deselectItem(at: indexPath, animated: false) }

override func viewDidLoad() {
//get view size
let fullsize = UIScreen.main.bounds.size
//get collectionViewCell layout
let layout = UICollectionViewFlowLayout()
//cell size
layout.itemSize = CGSize(width: 120, height: 30)
//mycollectionView size
myCollectionView = UICollectionView(frame: CGRect(x: 108, y: 70, width: fullsize.width - 70, height: fullsize.height - 180), collectionViewLayout: layout)
myCollectionView.delegate = self
myCollectionView.dataSource = self
myCollectionView.backgroundColor = UIColor.white
myCollectionView.register(MyCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
layout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 50);
layout.minimumLineSpacing = 10
layout.minimumInteritemSpacing = 0.5
myCollectionView.backgroundColor = UIColor(patternImage: myPag)
self.view.addSubview(myCollectionView)

}
}

MyCollectionViewCell 代码

class MyCollectionViewCell: UICollectionViewCell {
var titleLabel:UILabel!
override init(frame: CGRect) {
super.init(frame: frame)
// create UILabel
let w = Double(UIScreen.main.bounds.size.width)
titleLabel = UILabel(frame:CGRect(x: 0, y: 0, width: w/3 - 10.0, height: 30))
titleLabel.textAlignment = .center
titleLabel.textColor = UIColor.black
self.addSubview(titleLabel)
}
required init?(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
}

我想取消选择单元格,我应该怎么做?

有没有人能帮帮我谢谢老兄!!

如何让一个按钮可以选中所有的单元格?

最佳答案

您还需要在 viewDidLoad 中设置属性 allowsSelectionallowsMultipleSelection 而不是 didSelectItemAt indexPath,您还可以重复使用单元格,因此当您滚动 CollectionView 时,您选择的单元格会发生变化,以防止此问题创建一个 [IndexPath] 类型的实例,并像这样在您的 collectionView 方法中访问它。

var selectedCell = [IndexPath]()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! MyCollectionViewCell
cell.titleLabel?.text = arrayLocation[indexPath.row]
cell.titleLabel.font = UIFont.systemFont(ofSize: 24)
if selectedCell.contains(indexPath) {
cell.contentView.backgroundColor = .red
}
else {
cell.contentView.backgroundColor = .white
}
return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)!
selectedCell.append(indexPath)
cell.contentView.backgroundColor = .red
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)!
if selectedCell.contains(indexPath) {
selectedCell.remove(at: selectedCell.index(of: indexPath)!)
cell.contentView.backgroundColor = .white
}
}

关于Swift 如何取消选择 Collection Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541177/

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