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
}
集合委託
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
}
我嘗試使用本功能,但它不工作
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")
}
}
我想獲得細胞取消,我應該怎麼辦?
有沒有人可以幫助我PLZ感謝隊友!
如何讓一個按鈕可以選擇所有的單元?
是的它的工作!謝了哥們!!! – Jeff
對不起隊友我有另一個問題。 如何製作一個按鈕可以選擇所有的單元格? – Jeff
@Jeff給我一些時間來運行這個 –