如何重新加载UIPageViewController以在Swift中重新加载其视图
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何重新加载UIPageViewController以在Swift中重新加载其视图相关的知识,希望对你有一定的参考价值。
我正在使用表VC中嵌入的页面控制器。表VC显示了项目的详细信息,还包含集合视图控制器作为嵌入式控制器。所以现在当我选择任何Collection单元格时,它应该显示所选的单元格项目详细信息。我能够显示新选定项目的所有内容,但页面VC没有按照所选项目图像重新加载,它仍然显示最后一个项目图像。所以我被困在那里。我附上了页面VC和详细信息表视图的代码请让我知道这里处理它的方法。提前致谢。!!
//-----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
}
}
答案
我假设您在更新product
属性时希望它更改?
您需要在属性中添加didSet
,以更新当前显示的视图控制器。
以上是关于如何重新加载UIPageViewController以在Swift中重新加载其视图的主要内容,如果未能解决你的问题,请参考以下文章
如何在javascript中单击按钮重新加载网页,但在重新加载调用函数后