我如何 return 返回到第一个 collectionview 单元格项目?
How do I return back to the first collectionview cell item?
我正在 UICollectionViewCell
为我的移动应用程序工作。当您点击 crossBtn
时,它应该通过重新加载数据转到第一个 UICollectionViewCell
,但是 collectionView?.reloadData()
会使颜色混乱并且每个单元格不会一次保持一种颜色。相反,即使我在 didDeselectItemAt
方法中将颜色设置为 UIColor.clear
,它也会在单元格上显示多种颜色。
如何在用户点击 crossBtn
时 return 回到第一个 UICollectionViewCell
项目而不重新加载整个 UICollectionView
?
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return filters.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.hamburgerLabel.text = filters[indexPath.item]
// cell.hamburgerImageView.image = filterImages[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.backgroundColor = UIColor.gray.cgColor
setFilter(title: filterNames[indexPath.row])
toolBar.isHidden = true
yesAndNoToolBar.isHidden = false
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.backgroundColor = UIColor.clear.cgColor
collectionView.deselectItem(at: indexPath, animated: true)
}
@IBAction func crossBtn(_ sender: UIBarButtonItem) {
toolBar.isHidden = false
yesAndNoToolBar.isHidden = true
collectionView?.reloadData()
let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredVertically)
}
您可能不必重新加载数据。您可以使用以下方法滚动到给定的索引路径。
let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
func scrollToItem(at indexPath: indexPath,
at scrollPosition: .centeredVertically,
animated: true)
请注意,这与使用 selectItem(at:animated:scrollPosition:)
不同。
此外,您应该在 cellForItem
方法中设置遇到问题的颜色等,而不是在选择和取消选择委托方法中。这是因为单元格被重复使用,所以当您滚动时,您设置样式的单元格将在不同的地方使用。相反,使用选择和取消选择方法将选定的索引路径存储在变量(或多个数组)中,然后在 cellForItem
中检查此数组以确定是否将选定的样式应用于该单元格。这就是 collectionView.reloadData()
把事情搞砸的原因!
我正在 UICollectionViewCell
为我的移动应用程序工作。当您点击 crossBtn
时,它应该通过重新加载数据转到第一个 UICollectionViewCell
,但是 collectionView?.reloadData()
会使颜色混乱并且每个单元格不会一次保持一种颜色。相反,即使我在 didDeselectItemAt
方法中将颜色设置为 UIColor.clear
,它也会在单元格上显示多种颜色。
如何在用户点击 crossBtn
时 return 回到第一个 UICollectionViewCell
项目而不重新加载整个 UICollectionView
?
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return filters.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionViewCell
cell.hamburgerLabel.text = filters[indexPath.item]
// cell.hamburgerImageView.image = filterImages[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.backgroundColor = UIColor.gray.cgColor
setFilter(title: filterNames[indexPath.row])
toolBar.isHidden = true
yesAndNoToolBar.isHidden = false
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.backgroundColor = UIColor.clear.cgColor
collectionView.deselectItem(at: indexPath, animated: true)
}
@IBAction func crossBtn(_ sender: UIBarButtonItem) {
toolBar.isHidden = false
yesAndNoToolBar.isHidden = true
collectionView?.reloadData()
let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
self.collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.centeredVertically)
}
您可能不必重新加载数据。您可以使用以下方法滚动到给定的索引路径。
let indexPath = self.collectionView.indexPathsForSelectedItems?.last ?? IndexPath(item: 0, section: 0)
func scrollToItem(at indexPath: indexPath,
at scrollPosition: .centeredVertically,
animated: true)
请注意,这与使用 selectItem(at:animated:scrollPosition:)
不同。
此外,您应该在 cellForItem
方法中设置遇到问题的颜色等,而不是在选择和取消选择委托方法中。这是因为单元格被重复使用,所以当您滚动时,您设置样式的单元格将在不同的地方使用。相反,使用选择和取消选择方法将选定的索引路径存储在变量(或多个数组)中,然后在 cellForItem
中检查此数组以确定是否将选定的样式应用于该单元格。这就是 collectionView.reloadData()
把事情搞砸的原因!