Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 重新加载时UICollectionView出现闪烁问题_Ios_Swift_Uitableview_Uicollectionview_Uicollectionreusableview - Fatal编程技术网

Ios 重新加载时UICollectionView出现闪烁问题

Ios 重新加载时UICollectionView出现闪烁问题,ios,swift,uitableview,uicollectionview,uicollectionreusableview,Ios,Swift,Uitableview,Uicollectionview,Uicollectionreusableview,我正在开发一个应用程序,其中用户单击菜单项,tableView将根据所选项目滚动 但问题是在重新加载时collectionView会有一点抖动 这里是可见部分 func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if let firstVisibleIndexPath = self.tblRestaurantFeed.indexPa

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

但问题是在重新加载时collectionView会有一点抖动

这里是可见部分

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if let firstVisibleIndexPath = self.tblRestaurantFeed.indexPathsForVisibleRows?[0]{
    self.tableViewVisibleSection = firstVisibleIndexPath.section
}}
当变量更新时,我将重新加载collectionview并使用scroll to item

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)
            }
        }
}}


任何帮助都将不胜感激。

我认为问题在于您正在从动画块调用.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)
        }
}}
试试下面的一个

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)
    }
}

删除动画块后面临同样的问题。@RanjankumarSahu尝试使用didSelectItemAtIndexPath而不是willDisplay单元格。@RanjankumarSahu现在不使用.reloadData()调用进行尝试。使用更新的代码。如何选择集合视图项。这是indexPath:code
btnTag.isSelected=(indexPath.row==self.selectedTag)btnTag.layer.cornerRadius=btnTag.frame.height/2 btnTag.backgroundColor=btnTag.isSelected。蓝色:。清除
蓝色部分表示区段吗@Ranjan顶部灰色背景是集合视图,选择了蓝色项目,“说明”显示的位置是TableView的部分标题。您是否检查了此项?