【问题标题】:Flickering issue with UICollectionView while reloading重新加载时 UICollectionView 的闪烁问题
【发布时间】:2023-03-22 09:08:01
【问题描述】:

我正在开发一个应用程序,用户单击菜单项,tableView 应根据所选项目滚动。

但问题是collectionView在重新加载时闪烁了一下。

这里是可见部分。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let firstVisibleIndexPath = self.tblRestaurantFeed.indexPathsForVisibleRows?[0]{
    self.tableViewVisibleSection = firstVisibleIndexPath.section
}}

当变量更新时,我正在重新加载集合视图并使用滚动到项目

var tableViewVisibleSection:Int = 0{
didSet{
        UIView.animate(withDuration: 0.15, animations: {
            self.collectionView.reloadData()
        })
        let visibleIndexPaths = collectionView.indexPathsForVisibleItems
        let itemIndex = IndexPath(item: self.selectedTag, section: 0)
        if(!visibleIndexPaths.contains(itemIndex)){
            UIView.performWithoutAnimation {
                self.collectionView.scrollToItem(at: IndexPath(item: self.selectedTag, section: 0), at: .centeredHorizontally, animated: true)
            }
        }
}}

任何帮助将不胜感激。

【问题讨论】:

  • 那个蓝色的部分是表示Section吗? @Ranjan
  • 顶部灰色背景是带有蓝色选中项的集合视图,其中“描述”显示的是 tableview 的节标题
  • 你检查过这个answer吗?

标签: ios swift uitableview uicollectionview uicollectionreusableview


【解决方案1】:

我认为问题在于您正在从动画块调用 .reloadData()。请尝试下面的代码,让我知道它是否有效。

var tableViewVisibleSection: Int = 0 {
didSet {
        let visibleIndexPaths = collectionView.indexPathsForVisibleItems
        let itemIndex = IndexPath(item: self.selectedTag, section: 0)
        if(!visibleIndexPaths.contains(itemIndex)){
                self.collectionView.scrollToItem(at: IndexPath(item: self.selectedTag, section: 0), at: .centeredHorizontally, animated: false)
        }
}}

【讨论】:

  • 移除动画块后遇到同样的问题。
  • @RanjankumarSahu 尝试使用 didSelectItemAtIndexPath 而不是 willDisplay 单元格。
  • @RanjankumarSahu 现在尝试不调用 .reloadData()。使用更新的代码。
  • 如何选择集合视图项。这是cellForItemAt indexPath:代码btnTag.isSelected = (indexPath.row == self.selectedTag) btnTag.layer.cornerRadius = btnTag.frame.height / 2 btnTag.backgroundColor = btnTag.isSelected ? .blue : .clear
【解决方案2】:

试试下面的一个。

var currentMenu: Int = 0

override func viewDidLoad() {
        ...
        
        collectionView.selectItem(at: IndexPath(item: currentMenu, section: 0), animated: true, scrollPosition: [])
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        currentMenu = indexPath.row
        tableView.scrollToRow(at: IndexPath(item: 0, section: currentMenu), at: .top, animated: true)
        collectionView.selectItem(at: indexPath, animated: true, scrollPosition: .centeredHorizontally)
}

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let indexPath = tableView.indexPathsForVisibleRows?.first, abs(indexPath.section - currentMenu) == 1, scrollView.isDragging else { return }
        currentMenu = indexPath.section
        collectionView.selectItem(at: IndexPath(item: currentMenu, section: 0), animated: true, scrollPosition: .centeredHorizontally)
    }
}

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多