0

搜索搜索栏和标签栏隐藏和咬合时在应用程序上显示时出现问题,TabelViewCell但单元格像疯了一样重复但我不知道为什么?collectionViewtvOS

这是我的代码:

代表:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        ...
  
        let searchController = UISearchController(searchResultsController: searchVC)
        searchController.searchResultsUpdater = searchVC
        searchController.searchBar.placeholder = "Programs, Episodes, Presenters"
        searchController.overrideUserInterfaceStyle = .dark
        searchController.searchControllerObservedScrollView = searchVC.tblMainData

        let searchContainer = UISearchContainerViewController(searchController: searchController)
        searchContainer.tabBarItem = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.search, tag: 0)
        searchContainer.view.backgroundColor = .Dark_Blue
        searchContainer.didMove(toParent: searchVC)

        let tabbarController = UITabBarController()
        tabbarController.viewControllers = [homeVC, myListVC, programsVC, newsVC, videoVC, searchContainer]
        tabbarController.overrideUserInterfaceStyle = .dark
        
        self.window?.rootViewController = tabbarController
        self.window?.makeKeyAndVisible()

         return true

        }

搜索表视图:

extension SearchVC: UITableViewDataSource, UITableViewDelegate{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return arrSection?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: ItemSectionTVCell.className) as! ItemSectionTVCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        guard let tableViewCell = cell as? ItemSectionTVCell else { return }
        
        tableViewCell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.section)
        tableViewCell.collectionViewOffset = storedOffsets[indexPath.section] ?? 0
    }
    
    func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        guard let tableViewCell = cell as? ItemSectionTVCell else { return }
        storedOffsets[indexPath.section] = tableViewCell.collectionViewOffset
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if arrSection?[indexPath.section].title == "Recently Searched"{
            return 200
        }else if arrSection?[indexPath.section].title == "Episodes Found"{
            return 370
        }else{
            return tblMainData.frame.width / 7.5 + 25 + 100
        }
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tableView.frame.width, height: 50))
       
        let label = UILabel()
        label.frame = CGRect.init(x: 80, y: 5, width: headerView.frame.width-10, height: headerView.frame.height-10)
        label.text = arrSection?[section].title
        label.font = .boldSystemFont(ofSize: 31)
        label.textColor = .white
        headerView.addSubview(label)
    
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50
    }
    
    
    
}

搜索VC CollectionView:

extension SearchVC: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if isSearching && arrSection?[collectionView.tag].title == "Episodes Found" {
            return arrSection?[collectionView.tag].EpisodesFound?.count ?? 0
        }else if arrSection?[collectionView.tag].title == "Programs Found"{
            return arrSection?[collectionView.tag].ProgramsFound?.count ?? 0
        }else{
            return getRecentSearchesItems().count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
     
        if arrSection?[collectionView.tag].title == "Recently Searched" {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecentSearchCell.className, for: indexPath) as! RecentSearchCell
            configRecentSearch(collectionView, cell, indexPath: indexPath)
            return cell
        }
        
        if arrSection?[collectionView.tag].title == "Episodes Found" {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ShowItemCVCell.className, for: indexPath) as! ShowItemCVCell
            configEpisodesFoundCell(collectionView, cell, indexPath: indexPath)
            return cell
        }
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SqureEpisodeCVCell.className, for: indexPath) as! SqureEpisodeCVCell
        configSqureEpisodeCell(collectionView, cell, indexPath: indexPath)
        return cell
    
    
    }
    
    func configRecentSearch(_ collectionView: UICollectionView, _ cell: RecentSearchCell, indexPath: IndexPath){
        let item = getRecentSearchesItems()[indexPath.item]
        if let url = item.image {
            cell.imgBanner.sd_setImage(with: URL(string: url), completed: nil)
        }
        cell.lblTitle.text = item.title
        cell.lblName.text = item.name
        cell.lblEpisode.text = "E" + item.episode!
    }
       
    func configEpisodesFoundCell(_ collectionView: UICollectionView, _ cell: ShowItemCVCell, indexPath: IndexPath){
        let item = arrSection?[collectionView.tag].EpisodesFound?[indexPath.row]
        if let url = item?.thumbnails.hd{
            cell.imgBanner.sd_setImage(with: URL(string: url), completed: nil)
        }
        cell.lblName.text = item?.program.name
        cell.lblTitle.text = item?.postTitle
        cell.lblEpisode.text = "E\(item?.fields.episodeNumber ?? "E0")"
    }
    
   
    func configSqureEpisodeCell(_ collectionView: UICollectionView, _ cell: SqureEpisodeCVCell, indexPath: IndexPath){
        let item: ProgramFound? = arrSection?[collectionView.tag].ProgramsFound?[indexPath.row]
        if let url = item?.fields.programLogo.url{
            cell.imgEpisode.sd_setImage(with: URL(string: url), completed: nil)
        }
        cell.lblTitle.text = item?.name
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let title = arrSection?[collectionView.tag].title
        
        if title == "Recently Searched" {
            return CGSize(width: tblMainData.frame.width / 3 + 25, height: 200)
        }else if title == "Episodes Found" {
            return CGSize(width: tblMainData.frame.width / 6 + 40, height: 300)
        }else{
            return CGSize(width: tblMainData.frame.width / 7.5 + 25, height: tblMainData.frame.width / 7.5 + 25 + 53)
        }
    
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 40
    }

    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vcVidepPlayer = self.storyboard?.instantiateViewController(withIdentifier: "VideoPlayerVC") as! VideoPlayerVC

        if collectionView.tag == 0 {
            
            if let selected = arrSection?[collectionView.tag].EpisodesFound?[indexPath.row] {
                
                addSerchToRecentSearches(vc: self, episode: selected.episodeNumber, image: selected.thumbnails.hd, name: selected.program.name, title: selected.postTitle, videoLink: selected.fields.brightcoveVideoLink)
                
                addProgramToRecentlyViewed(vc: self, program_episode: selected.episodeNumber, program_image: selected.thumbnails.hd, program_name: selected.program.name, program_title: selected.postTitle, program_termId: String(selected.program.termID), program_time: selected.fields.duration, brightcoveVideoLink: selected.fields.brightcoveVideoLink, total_episode: String(selected.program.count), program_casting_time: selected.fields.duration, episode_description: selected.program.programDescription, program_watch_time: "00:00")

            }else{
                
                vcVidepPlayer.videoID = getRecentSearchesItems()[indexPath.item].videoLink
                vcVidepPlayer.video_duration =  "00:00"
                self.present(vcVidepPlayer, animated: true, completion: nil)
            }
            
        } else if collectionView.tag == 1 {
            let vcProgramDetails = self.storyboard?.instantiateViewController(withIdentifier: ProgramDetailsVC.className) as! ProgramDetailsVC
            vcProgramDetails.itemEpisodetermId = arrSection?[collectionView.tag].ProgramsFound?[indexPath.row].termID
            vcProgramDetails.programType =  arrSection?[collectionView.tag].ProgramsFound?[indexPath.row].fields.programType
            vcProgramDetails.programImg = arrSection?[collectionView.tag].ProgramsFound?[indexPath.row].fields.programLogo.url
            self.present(vcProgramDetails, animated: true, completion: nil)
        }
        
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didUpdateFocusIn context: UICollectionViewFocusUpdateContext, with coordinator: UIFocusAnimationCoordinator) {
        if let previousIndexPath = context.previouslyFocusedIndexPath{
            if let cell = collectionView.cellForItem(at: previousIndexPath) as? RecentSearchCell {
                
                cell.viewMain.layer.borderWidth = 0.0
                cell.viewMain.layer.shadowRadius = 0.0
                cell.viewMain.layer.shadowOpacity = 0
            }else if let cell = collectionView.cellForItem(at: previousIndexPath) as? ShowItemCVCell {
                
                cell.imgBanner.layer.borderWidth = 0.0
                cell.imgBanner.layer.shadowRadius = 0.0
                cell.imgBanner.layer.shadowOpacity = 0
                cell.lblTitle.labelize = true

            }else if let cell = collectionView.cellForItem(at: previousIndexPath) as? SqureEpisodeCVCell {
                
                cell.imgEpisode.layer.borderWidth = 0.0
                cell.imgEpisode.layer.shadowRadius = 0.0
                cell.imgEpisode.layer.shadowOpacity = 0
            }
        }
        
        if let indexPath = context.nextFocusedIndexPath{
            if let cell = collectionView.cellForItem(at: indexPath) as? RecentSearchCell{
               
                
                cell.viewMain.layer.borderWidth = 5.0
                cell.viewMain.layer.cornerRadius = 23
                cell.viewMain.layer.borderColor = UIColor.white.cgColor
            }else if let cell = collectionView.cellForItem(at: indexPath) as? ShowItemCVCell{
                
                
                cell.imgBanner.layer.borderWidth = 5.0
                cell.imgBanner.layer.cornerRadius = 12.0
                cell.imgBanner.layer.borderColor = UIColor.white.cgColor
                cell.lblTitle.labelize = false
                
            }else if let cell = collectionView.cellForItem(at: indexPath) as? SqureEpisodeCVCell{
                
                cell.imgEpisode.layer.borderWidth = 5.0
                cell.imgEpisode.layer.cornerRadius = 12.0
                cell.imgEpisode.layer.borderColor = UIColor.white.cgColor
                
            }
            
            collectionView.scrollToItem(at: indexPath, at: [.centeredHorizontally, .centeredVertically], animated: true)
        }
    }
    
}

向下滚动时出现问题的屏幕截图:

搜索时

向下滚动后

感谢您的任何建议。

4

0 回答 0