【发布时间】:2018-01-18 12:43:31
【问题描述】:
我有一个数组,其中包含先前选择的单元格的位置。 当我的弹出窗口出现,就像我在中所做的那样:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
我尝试了不同的方法,例如:
if (indexPath.row == 0 && indexPath.section == 0){
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredHorizontally)
}
或者:
let indexPathForFirstRow = IndexPath(item: 0, section: 0)
collectionView.cellForItem(at: indexPathForFirstRow)?.isSelected = true
但没有任何效果(或者我看不到它......)
你有什么帮助吗? 谢谢!
编辑:
这是我现在要做的:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// get a reference to our storyboard cell
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell
// Use the outlet in our custom class to get a reference to the UILabel in the cell
if (swipeDown![2][0] == (settings!.rowSelect) && indexPath.row == 4 && indexPath.section == 4) {
let indexPathspec = IndexPath(row: 4, section: 4)
var cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPathspec) as! MyCollectionViewCell
cell.isSelected = true
cell.myLabel.text = self.items[indexPath.item]
//cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 2
print("test")
return cell
}
cell.myLabel.text = self.items[indexPath.item]
cell.backgroundColor = UIColor(red:0.13, green:0.37, blue:0.58, alpha:0.7)
cell.layer.borderColor = UIColor.black.cgColor
cell.layer.borderWidth = 2
print(cell.isSelected)
print("11")
return cell
}
在 MyCollectionViewCell 中有这个
import UIKit
class MyCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var myLabel: UILabel!
override var isSelected: Bool{
didSet{
//super.isSelected = true
self.contentView.backgroundColor = self.isSelected ? .blue : .green
}
}
}
单元格是蓝色的,所以它已被选中,但一旦完成,我就无法取消选中它。 启动后我选择的单元格没有问题,我可以毫无问题地选择和取消选择它们。
【问题讨论】:
-
检查您的单元格是否可选择
-
@ReinierMelian 我以前可以选择它,为什么现在不能?只想以编程方式完成。
标签: ios swift swift3 uicollectionviewcell