[英]How to reload tableview using view controller (swift)
我对此很陌生。 我花了几个小时来浏览关于这个主题的各种问题,但找不到适合我问题的答案。
我有一个视图控制器(不是 tableviewcontroller),它有一个 tableview 作为子视图。 我的问题是如何在视图控制器的 :viewwillappear 方法中重新加载表数据。 在方法内部,我无法“访问” tableview 实例。 (我也知道我不能使用 reloaddata() 因为我没有使用 tableviewcontroller)。
你知道任何简单的解决方案吗? (我非常假设协议可以在这里提供帮助?)
这是简短的代码:
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
var Person_data = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
let tb: UITableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
tb.dataSource = self
tb.delegate = self
super.view.addSubview(tb)
fetch_data()
}
func tableView(tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
let rows = Person_data.count
return rows
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell: UITableViewCell = UITableViewCell()
cell.textLabel?.text = Person_data[indexPath.row].name
return cell
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetch_data()
// reload at this point
}
func fetch_data() {
let tb_fetchRequest = NSFetchRequest(entityName: "Person")
do {
let tb_fetchResults = try my_moc.executeFetchRequest(tb_fetchRequest) as? [Person]
Person_data = tb_fetchResults!
} catch let error as NSError {
print("request error: \(error)")
}
}
}
你的问题是tb
是一个局部变量,所以它在viewDidLoad()
之外不可见。
如果您将tb
视图控制器的属性(又名实例变量),那么您可以从控制器的任何方法中使用它:
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
var Person_data = [Person]()
private var tb: UITableView? // Now the scope of tb is all of ViewController3...
override func viewDidLoad() {
super.viewDidLoad()
// ...so it is visible here...
tb = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
...
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetch_data()
tb?.reloadData() // ...and it is also visible here.
}
请注意,在您的代码中, tb
是局部变量这一事实并不意味着您的UITableView
对象在viewDidLoad()
完成时消失。 这意味着您对UITableView
的引用消失了。 表视图对象本身仍然存在,因为它已添加到您的视图层次结构中,但您不再引用它,因为您的变量超出了范围。
要进一步了解变量与其引用的对象之间的区别,请搜索“范围”和“通过引用传递”。
您可以在viewWillAppear:
访问您的tableView
IBOutlet
,您也可以调用reloadData
。 我不确定为什么你认为你不能,但它应该可以正常工作。
您必须创建一个带有 tableView 链接的变量,并在 viewWillAppear 之前创建它。
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
private let tableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
var Person_data = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
super.view.addSubview(tb)
fetch_data()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetch_data()
tableView.reloadData()
}
}
class ViewController3: UIViewController, UITableViewDataSource, UITableViewDelegate {
private let tableView = UITableView(frame: CGRect(x: 10, y: 50, width: 200, height:600), style: UITableViewStyle.Plain)
var Person_data = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
super.view.addSubview(tb)
fetch_data()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetch_data()
tableView.reloadData()
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//This is outlet of tableView...
@IBOutlet public var userTableView : UITableView!
override func viewDidLoad() {
super.viewDidLoad()
userTableView.delegate = self
userTableView .datasource = self
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
fetchData()
}
func fetchData(){
//Here you can fetch your required data
//Here the tableView will be reload...
userTableView.reloadData()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.