Ios 在didSelectItemAt中选择单元格时,取消选择cellForItemAt中的单元格
我有一个VariableSelectedIndexPath,它获取从上一个视图控制器中选择的indexPath。我正在获取当前视图控制器集合视图中单元格所需的背景色。但是,当我在“集合”视图中选择另一个单元格时,上一个视图控制器的选定单元格保持不变,不会被取消选择。现在我有两个背景色的单元格。下面是我的代码Ios 在didSelectItemAt中选择单元格时,取消选择cellForItemAt中的单元格,ios,swift,uicollectionview,uicollectionviewcell,Ios,Swift,Uicollectionview,Uicollectionviewcell,我有一个VariableSelectedIndexPath,它获取从上一个视图控制器中选择的indexPath。我正在获取当前视图控制器集合视图中单元格所需的背景色。但是,当我在“集合”视图中选择另一个单元格时,上一个视图控制器的选定单元格保持不变,不会被取消选择。现在我有两个背景色的单元格。下面是我的代码 var selectedIndexPath = IndexPath() override func viewDidLoad() { super.viewDidLoad()
var selectedIndexPath = IndexPath()
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.allowsMultipleSelection = false
self.collectionView.allowsSelection = true
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! TestCollectionViewCell
if (indexPath == selectedIndexPath)
{
cell.backgroundColor = UIColor.white
}
else
{
cell.backgroundColor = UIColor.clear
}
collectionView.allowsMultipleSelection = false
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.white
collectionView.allowsMultipleSelection = false
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
let cell = collectionView.cellForItem(at: indexPath)
cell?.backgroundColor = UIColor.clear
collectionView.allowsMultipleSelection = false
}
在didSelectItemAt中选择新单元格时,如何取消选择cellForItemAt中的单元格。提前感谢。首先,您不需要为此覆盖“取消选择”。您只需在didSelectItem中选择当前项目的同时取消选择以前选择的项目。为此,您可以选择将单元格中的状态保持为:
func changeToSelectedState() {
self.backgroundColor = UIColor.white
}
func changeToUnselectedState() {
self.backgroundColor = UIColor.clear
}
或者,您可以选择在控制器本身中写入相同的内容。然后您需要按照以下方式执行取消选择和选择
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let previousSelectedCell = collectionView.cellForItem(at: selectedIndexPath)
previousSelectedCell.changeToUnselectedState()
let currentCell = collectionView.cellForItem(at: indexPath)
currentCell.changeToSelectedState()
selectedIndexPath = indexPath
}
首先,您不需要为此重写DIDDENSELECT。您只需在didSelectItem中选择当前项目的同时取消选择以前选择的项目。为此,您可以选择将单元格中的状态保持为:
func changeToSelectedState() {
self.backgroundColor = UIColor.white
}
func changeToUnselectedState() {
self.backgroundColor = UIColor.clear
}
或者,您可以选择在控制器本身中写入相同的内容。然后您需要按照以下方式执行取消选择和选择
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let previousSelectedCell = collectionView.cellForItem(at: selectedIndexPath)
previousSelectedCell.changeToUnselectedState()
let currentCell = collectionView.cellForItem(at: indexPath)
currentCell.changeToSelectedState()
selectedIndexPath = indexPath
}
首先,在Interface Builder中设置allowsMultipleSelection,并删除在代码中设置它的所有冗余实例
您必须更新selectedIndexPath变量。直接操纵细胞总是一个坏主意。
重新加载电池更可靠
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
guard selectedIndexPath != indexPath else { return }
let indexPathsToUpdate = [selectedIndexPath, indexPath]
selectedIndexPath = indexPath
tableView.reloadRows(at: indexPathsToUpdate, with: .none)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
guard selectedIndexPath == indexPath else { return }
selectedIndexPath = IndexPath()
tableView.reloadRows(at: [indexPath], with: .none)
}
只有一个问题:如果要有一个空选择,必须将selectedIndexPath声明为可选并正确处理。首先在Interface Builder中设置allowsMultipleSelection,并删除在代码中设置它的所有冗余实例
您必须更新selectedIndexPath变量。直接操纵细胞总是一个坏主意。
重新加载电池更可靠
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
guard selectedIndexPath != indexPath else { return }
let indexPathsToUpdate = [selectedIndexPath, indexPath]
selectedIndexPath = indexPath
tableView.reloadRows(at: indexPathsToUpdate, with: .none)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath)
{
guard selectedIndexPath == indexPath else { return }
selectedIndexPath = IndexPath()
tableView.reloadRows(at: [indexPath], with: .none)
}
只有一个问题:如果您想要一个空的选择,您必须将selectedIndexPath声明为可选,并正确处理它。非常感谢。这是有道理的:它工作得很好,但唯一的问题是,如果您选择同一个单元格两次,然后选择另一个单元格,它将不工作,并在collectionView.reloadItemsat:indexPathsToUpdate in DidSelectItem中引发错误。我知道,这就是我提到的问题。我会放弃didDeselectItemAt,只使用DidSelectItem来欣赏它。这是有道理的:它工作得很好,但唯一的问题是,如果您选择同一个单元格两次,然后选择另一个单元格,它将不工作,并在collectionView.reloadItemsat:indexPathsToUpdate in DidSelectItem中引发错误。我知道,这就是我提到的问题。我会删除didSelectItemAt,只使用didSelectItemAt