如何重新加载 UIPageViewController 以在 Swift 中重新加载其视图

Posted

技术标签:

【中文标题】如何重新加载 UIPageViewController 以在 Swift 中重新加载其视图【英文标题】:How to reload UIPageViewController to reload its views in Swift 【发布时间】:2018-12-24 04:02:39 【问题描述】:

我正在使用嵌入在表 VC 中的页面控制器。表 VC 显示了项目的详细信息,还包含嵌入的集合视图控制器 一。所以现在当我选择任何集合单元格时,它应该显示选定的单元格项目详细信息。 我能够显示新选定项目的所有内容,但 Page VC 没有根据选定项目图像重新加载,它仍然显示最后一个项目图像。 所以我被困在那里。我附上了 Page VC 和 Detail Table View 的代码 请让我知道这里处理它的方法。提前致谢!!

//-----PAGE VC CODE------
import UIKit
import Firebase

protocol ProductImagesPageVCDelegate: class

    func setupPageController(numberOfPages: Int)
    func turnPageController(to index: Int)


class ProductImagesPageVC: UIPageViewController 

    var product: Product!

    weak var pageViewControllerDelegate: ProductImagesPageVCDelegate?

    struct StoryBoard 
        static let productImageVC = "ProductImageVC"
    

    lazy var controllers: [UIViewController] = 

        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controllers = [UIViewController]()

        if let imageLinks = self.product.imageLinks
        
            for imageLink in imageLinks
            
                let productImageVC = storyboard.instantiateViewController(withIdentifier: StoryBoard.productImageVC)
                controllers.append(productImageVC)
            
        

        self.pageViewControllerDelegate?.setupPageController(numberOfPages: controllers.count)

        return controllers
    ()

    override func viewDidLoad() 
        super.viewDidLoad()

//        if #available(ios 11.0, *) 
//            contentInsetAdjustmentBehavior = .never
//         else 
//            automaticallyAdjustsScrollViewInsets = false
//        
        automaticallyAdjustsScrollViewInsets = false
        dataSource = self
        delegate = self

        self.turnToPage(index: 0)


    


    func turnToPage(index: Int)
    
        let controller = controllers[index]
        var direction = UIPageViewControllerNavigationDirection.forward

        if let currentVC = viewControllers?.first
        
            guard let currentIndex = controllers.index(of: currentVC) else return
            if currentIndex > index
            
                direction = .reverse
            
        

        self.configuewDisplaying(viewController: controller)

        setViewControllers([controller], direction: direction, animated: true, completion: nil)

    

    func configuewDisplaying(viewController: UIViewController)
    
        for (index, vc) in controllers.enumerated()
        
            if viewController === vc 
                if let productImageVC = viewController as? ProductImageVC
                
                    productImageVC.imageLink = self.product.imageLinks?[index]

                    self.pageViewControllerDelegate?.turnPageController(to: index)
                
            
        
    




extension ProductImagesPageVC: UIPageViewControllerDataSource

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? 

        if let index = controllers.index(of: viewController)
        
            if index < controllers.count - 1
            
                return controllers[index + 1]
            
        


        return controllers.first
    

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? 

        if let index = controllers.index(of: viewController)
        
            if index > 0
            
                return controllers[index - 1]
            
        

        return controllers.last
    


extension ProductImagesPageVC: UIPageViewControllerDelegate


    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) 

        self.configuewDisplaying(viewController: pendingViewControllers.first as! ProductImageVC)
    

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) 

        if !completed
        
            self.configuewDisplaying(viewController: previousViewControllers.first as! ProductImageVC)
        
    




//-----------Tabel VIEW Controllers-----
import UIKit

class ProductDetailTVC: UITableViewController 

    @IBOutlet var productImagesHeaderView: ProductImagesHeaderView!
    var product: Product!
    var products: [Product]?
    private var selectedProduct: Product?
    struct Storyboard 

        static let productDetailCell = "ProductDetailCell"
        static let buyButtonCell = "BuyButtonCell"
        static let showProductDetailCell = "ShowProductDetailCell"
        static let suggestionTableCell = "SuggestionTableCell"
        static let showImagesPageVC = "ShowProductImagesPageVC"
        static let showProductDetail = "ShowProductDetail"
    

    override func viewDidLoad() 
        super.viewDidLoad()

        title = product.name
        fetchProducts()
        tableView.estimatedRowHeight = tableView.rowHeight
        tableView.rowHeight = UITableViewAutomaticDimension

    
    func fetchProducts()
    
        Product.fetchProducts  (products) in
            self.products = products
            if let index = self.products?.index(where: $0 === self.product) 
                self.products?.remove(at: index)
            
            self.tableView.reloadData()
        

    




    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int 
        // #warning Incomplete implementation, return the number of sections
        return 1
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
        // #warning Incomplete implementation, return the number of rows
        return 4
    


    fileprivate func extractedFunc() -> UITableViewCell 
        return UITableViewCell()
    

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 

        if indexPath.row == 0
        
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.productDetailCell, for: indexPath) as! ProductDetailCell

            cell.product = product
            cell.selectionStyle = .none

            return cell

         else if indexPath.row == 1
        
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.buyButtonCell, for: indexPath) as! BuyButtonCell

            cell.product = product
            cell.selectionStyle = .none

            return cell

         else if indexPath.row == 2
        
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.showProductDetailCell, for: indexPath)
             cell.selectionStyle = .none

            return cell

        
        else
        
            let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.suggestionTableCell, for: indexPath) as! SuggestionTableCell
            //cell.selectionStyle = .none

            return cell

        
    


    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat 

        if indexPath.row == 3
        
            return tableView.bounds.width + 68
        
        else
        
            return UITableViewAutomaticDimension
        
    


    //Mark: - UITabeleViewDelegate

    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) 

        if indexPath.row == 3
        
            if let cell = cell as? SuggestionTableCell
            
                cell.collectionView.delegate = self
                cell.collectionView.dataSource = self
                cell.collectionView.reloadData()
                cell.collectionView.isScrollEnabled = false
            
        
    






    override func prepare(for segue: UIStoryboardSegue, sender: Any?) 

        if segue.identifier == Storyboard.showImagesPageVC
        
            if let imagesPageVC = segue.destination  as? ProductImagesPageVC
            
                imagesPageVC.product = product

                imagesPageVC.pageViewControllerDelegate = productImagesHeaderView
            
        
    





//MARK: - UICollectionViewDataSource
extension ProductDetailTVC : UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int 
        return 1
    

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 

        return 4
    

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SuggestionCollectionViewCell", for: indexPath) as! SuggestionCollectionViewCell

        guard let products = products else return cell
        let randomProduct = Int(arc4random_uniform(UInt32(products.count)))
        cell.product = products[randomProduct]


        return cell
    

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) 
        guard let selectedProduct = products?[indexPath.item] else return
        self.selectedProduct = selectedProduct
        self.product = selectedProduct
        navigationItem.title = selectedProduct.name

        self.tableView.reloadData()
        collectionView.reloadData()
        self.reloadInputViews()

    





//MARK: - UICollectionViewDelegate

extension ProductDetailTVC : UICollectionViewDelegate





//MARK: - UICOLLECTIONVIEWDELEGATEFLOWLAYOUT

extension ProductDetailTVC : UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize 

        if let layout = collectionViewLayout as? UICollectionViewFlowLayout
        
            layout.minimumLineSpacing = 5.0
            layout.minimumInteritemSpacing = 2.5

            let itemWidth = (collectionView.bounds.width - 5.0) / 2.0

            return CGSize(width: itemWidth, height: itemWidth)
        

        return CGSize.zero
    

【问题讨论】:

任何人都可以提出解决方案? 【参考方案1】:

我假设您希望在更新 product 属性时对其进行更改?

您需要将didSet 添加到更新当前显示的视图控制器的属性中。

【讨论】:

是的..我需要更改新产品vc页面上的图像 能否请您突出显示您所指修改的行的哪一部分代码 任何人都可以在这里提供帮助 你有什么建议吗? @聪明

以上是关于如何重新加载 UIPageViewController 以在 Swift 中重新加载其视图的主要内容,如果未能解决你的问题,请参考以下文章

如何在javascript中单击按钮重新加载网页,但在重新加载调用函数后

如何检查页面重新加载是不是完成

如何重新加载 UIPageViewController 以在 Swift 中重新加载其视图

单击reactjs时如何重新加载表格

如何使用 Spring 重新加载属性?

如何在不重新加载 visibleCell 的情况下在 collectionView 上重新加载数据