简体   繁体   English

隔离前如何在Main View Controller中加载Ta​​ble View

[英]How to load Table View in Main View Controller before segueing

Working on Swift 3 Xcode 9.1 在Swift 3 Xcode 9.1上工作

I have two view controllers (one MainView and one Table View) 我有两个视图控制器(一个MainView和一个Table View)

Problem: The Table View loads data from an API and displays it, however it take too long. 问题:“表视图”从API加载数据并显示它,但是花费的时间太长。 I'm trying to have the load occur on the Main Controller. 我试图使负载发生在主控制器上。

I tried creating a custom method How to instantiate and load a view controller before segueing to it using swift 我试图创建一个自定义方法, 如何在使用swift进行选择之前先实例化和加载视图控制器

However the Table View has too many functions and I'm not sure how to call it. 但是,表视图具有太多的功能,我不确定如何调用它。

Table View Code: 表格查看代码:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    return cell
}

func loadData(){
 //loads data 
 DispatchQueue.main.sync(execute:{
    self.tableView.reloadData()
}

Question: Is creating a custom method the best way or is there another way? 问题:创建自定义方法是最好的方法还是还有另一种方法? Please note I'm trying to do all this is the Main View Controller so the data can load once I segue to the Table View. 请注意,我正在尝试使用Main View Controller进行所有操作,因此一旦我选择到Table View,就可以加载数据。

Try putting the API call in the ViewDidAppear Method of the Table View Controller Class. 尝试将API调用放入“表视图控制器类”的ViewDidAppear方法中。 That way the app will segue to the View Controller then call the data. 这样,应用程序将选择到View Controller,然后调用数据。 The app will appear faster but take the same amount of time. 该应用程序的显示速度更快,但所需的时间相同。 This way you can also manage timeouts and default data for the ViewDidLoad. 这样,您还可以管理ViewDidLoad的超时和默认数据。

Thank you all for your help! 谢谢大家的帮助!

I actually incorporated FryAnEgg's comment and created a Global Variable in my Main VC to load in the API. 我实际上结合了FryAnEgg的注释,并在我的主VC中创建了一个全局变量以加载到API中。

Main VC: 主VC:

var Global X  

class Main VC: UIViewController {
   loadDataFunc(){
      GlobalX.append(data)
     }
 }

TableView: TableView:

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  return GlobalX.count //calling Global Variable
  }
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  return cell
  }

I then called the Global Variable in the Table View Controller and it worked ! 然后,我在表视图控制器中调用了全局变量,它起作用了!

Thanks again everyone 再次感谢大家

To load tablview in main view before segueing you can use NSNotificationCenter. 要在搜索前在主视图中加载tablview,可以使用NSNotificationCenter。 It is call in ViewDidLoad so it will reload data. 在ViewDidLoad中调用它,因此它将重新加载数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用swift进行选择之前,如何实例化和加载视图控制器 - How to instantiate and load a view controller before segueing to it using swift 在Swift中从文本字段中选择使用表视图的视图控制器时出现错误 - Error when segueing to view controller with Table View from Textfield in Swift 如何在主视图控制器加载之前加载位置? - How to load location before main view controller loads? 将视图控制器导入另一个控制器进行segueing是否安全? - Is it safe to import a view controller to another controller for segueing? 使用导航控制器在视图控制器之间进行隔离 - segueing between view controllers with navigation controller segueing到不同的视图控制器后动画停止 - Animation stops after segueing to different view controller 将UISearchBar与表视图控制器一起使用并转换到另一个视图时显示问题 - Display issue when using UISearchBar with a table view controller and segueing to another view UIButton在第一次加载时不响应触摸,但是在从另一个视图控制器回退后可以工作 - UIButton doesn't respond to touch on first load, but works after segueing back from another view controller 在导航控制器中重新选择视图控制器时,整个屏幕为空白 - When segueing back to view controller in navigation controller, entire screen is blank 如何在加载主视图之前显示 controller 视图? - How to present a view controller before main one is loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM