[英]swift - coredata refresh tableview
I have a tableview to display product, and a view to add a new product. 我有一个用于显示产品的表格视图和一个用于添加新产品的视图。
In AddProductViewController, after adding product data to coredata I dismiss the controller and return to my ProductTableView. 在AddProductViewController中,将产品数据添加到coredata后,我关闭了控制器并返回到我的ProductTableView。
navigationController!.popViewControllerAnimated(true)
In ProductTableView I added refreshcontroller 在ProductTableView中,我添加了refreshcontroller
override func viewDidLoad() {
super.viewDidLoad()
product = fetchProducts()
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: "refreshProducts", forControlEvents: UIControlEvents.ValueChanged)
refreshControl?.beginRefreshing()
}
func refreshProducts(){
product = fetchProducts()
}
func fetchProducts() -> [Product] {
let request = NSFetchRequest(entityName: "Product")
let sortDescriptor = NSSortDescriptor(key: "productName", ascending: true)
request.sortDescriptors = [sortDescriptor]
let product = (try! context!.executeFetchRequest(request)) as! [Product]
self.view.hideLoading()
self.refreshControl?.endRefreshing()
return product
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.refreshControl?.beginRefreshing()
self.tableView.reloadData()
}
What I have is: 我所拥有的是:
1. Tableview is not refreshed after dismissing the second viewcontroller 1.关闭第二个ViewController后,不会刷新Tableview
2. There is a frozen refresh icon. 2.有一个冻结的刷新图标。
3. when I launch my app, I have a refresh wheel spinning and not disappearing. 3.当我启动我的应用程序时,我的刷新轮在旋转并且没有消失。
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
refreshControl?.addTarget(self, action: "refreshProducts", forControlEvents: UIControlEvents.ValueChanged)
}
func refreshProducts(){
product = fetchProducts()
self.tableView.reloadData()
}
func fetchProducts() -> [Product] {
let request = NSFetchRequest(entityName: "Product")
let sortDescriptor = NSSortDescriptor(key: "productName", ascending: true)
request.sortDescriptors = [sortDescriptor]
let product = (try! context!.executeFetchRequest(request)) as! [Product]
self.view.hideLoading()
self.refreshControl?.endRefreshing()
return product
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.refreshControl?.beginRefreshing()
refreshProducts()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.