【发布时间】:2015-10-28 20:40:42
【问题描述】:
我对此很陌生。我花了几个小时来解决有关此主题的各种问题,但找不到适合我的问题的答案。
我有一个带有 tableview 作为子视图的 viewcontroller(不是 tableviewcontroller)。我的问题是如何在视图控制器的 :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)")
}
}
}
【问题讨论】:
标签: ios swift uitableview reloaddata