[英]Recall didSelectItemAt on collectionView
In my project, there is label inside the collectionView cell that when user taps on it, the color of the label will change I did that with these codes在我的项目中,collectionView 单元格中有 label,当用户点击它时,label 的颜色会改变我用这些代码做到了
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
cell.selectCell()
}
}
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
cell.deSelectCell()
}
}
Now, I want when user open this page and he/she already tap on any of them before, the selectCell
will be called again.现在,我希望当用户打开此页面并且他/她之前已经点击过其中任何一个时,将再次调用
selectCell
。 I can say in the cellForRowAt
if the cell is the selected cell, call that function, but it wont behave like didSelectItemAt
and didSelectItemAt
.我可以在
cellForRowAt
中说,如果单元格是选定的单元格,则调用 function,但它的行为不会像didSelectItemAt
和didSelectItemAt
。 So how can I set when the page gets opened, it call didSelectItemAt
and tap on the specific index?那么如何设置页面打开时,它调用
didSelectItemAt
并点击特定索引? Then I when I add on the another cell, didSelectItemAt is also called.然后,当我添加另一个单元格时,也调用了 didSelectItemAt。 Thank you so much
太感谢了
here is my sulation, I don't know it's acceptable or not这是我的建议,我不知道它是否可以接受
for example, I set the selected row to index.row == 2例如,我将所选行设置为 index.row == 2
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(reusableType: CaseCell.self, indexPath: indexPath)
cell.configure(text: cases[indexPath.row])
if indexPath.row == 2 {
cell.selectCell()
} else {
cell.deSelectCell()
}
return cell
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
for index in 0..<cases.count {
let index = IndexPath(row: index, section: 0)
let deselctCell = collectionView.cellForItem(at: index) as? CaseCell
deselctCell?.deSelectCell()
}
cell.selectCell()
}
}
A better approach would be to use both didSelect
and didDeselect
method to handle the selection and deselection of cells instead of interating through the entire collection of cells.更好的方法是同时使用
didSelect
和didDeselect
方法来处理单元格的选择和取消选择,而不是遍历整个单元格集合。
public func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
cell.deSelectCell()
}
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let cell = collectionView.cellForItem(at: indexPath) as? CaseCell {
cell.selectCell()
}
}
My suggestion is to create a property for the currently selected index path.我的建议是为当前选择的索引路径创建一个属性。 You can set it to a default value
您可以将其设置为默认值
var selectedIndexPath : IndexPath? = IndexPath(row: 2, section: 0)
The most reliable way is to modify the cells only in cellForItemAt
, nowhere else.最可靠的方法是仅在
cellForItemAt
中修改单元格,其他任何地方都没有。 Depending on selectedIndexPath
select or deselect the sell.取决于
selectedIndexPath
select 或取消选择出售。
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(reusableType: CaseCell.self, indexPath: indexPath)
cell.configure(text: cases[indexPath.row])
indexPath == selectedIndexPath ? cell.selectCell() : cell.deSelectCell()
return cell
}
In didSelectItemAt
change the value of selectedIndexPath
and reload both the current and the previous cell.在
didSelectItemAt
中更改selectedIndexPath
的值并重新加载当前和上一个单元格。
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
{
if selectedIndexPath != indexPath {
let indexPathsToReload = selectedIndexPath == nil ? [indexPath] : [selectedIndexPath!, indexPath]
selectedIndexPath = indexPath
collectionView.reloadItems(at: indexPathsToReload)
} else {
selectedIndexPath = nil
collectionView.reloadItems(at: [indexPath])
}
}
didDeselectedItemAt
is not needed. didDeselectedItemAt
。
The code considers also the case of an empty selection.该代码还考虑了空选择的情况。 If you don't want this declare
selectedIndexPath
as non-optional如果您不希望此声明
selectedIndexPath
为非可选
var selectedIndexPath = IndexPath(row: 2, section: 0)
and change didSelectItemAt
to并将
didSelectItemAt
更改为
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
{
guard selectedIndexPath != indexPath else { return }
let indexPathsToReload = [selectedIndexPath, indexPath]
selectedIndexPath = indexPath
collectionView.reloadItems(at: indexPathsToReload)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.