目前我有以下代码可以保存一个对象,但是我想更新/重新加载表格 View 。该按钮未附加到单元格/行,它位于我的导航 Controller 的右上角(加号图标)
注意:所有事情都发生在同一个场景中,因此任何附加到 segue 的事件(我可以在其中重新加载表数据)都是不可能的。
@IBAction func addWeek (sender: UIButton){
let newnumber:Int = routineWeeks.count + 1
// save data using cor data managed object context
if let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)?.managedObjectContext {
week = NSEntityDescription.insertNewObjectForEntityForName("Weeks", inManagedObjectContext: managedObjectContext) as! Weeks
week.weekNumber = newnumber
do {
try managedObjectContext.save()
} catch {
print(error)
return
}
}
//currently not reloading table with new data
tableView.reloadData()
//print information in console
print("end of save function, dismiss controller")
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)
// Configure the cell...
cell.textLabel?.text = "Week \(routineWeeks[indexPath.row].weekNumber)"
return cell
}
更新 感谢 Stackoverflow 社区为我指明了正确的方向。
routineWeeks.append(week)
print("this is a new week:\(week)")
tableView.reloadData()
最佳答案
我不知道它是否对你有帮助,但我遇到了同样的问题,我添加了 Refresher (增加“拉动刷新”功能)
在我的类(class):
override func viewDidLoad() {
super.viewDidLoad()
// Pull to refresh - DEBUT
tableFiches.addPullToRefreshWithAction {
NSOperationQueue().addOperationWithBlock {
//sleep(1)
NSOperationQueue.mainQueue().addOperationWithBlock {
self.loadMyData() // My func here to load data
self.tableFiches.stopPullToRefresh()
}
}
}
// Pull to refresh - FIN
}
func loadMyData(){
// request here
let recupJSON = JSON(value) // my data
if let resData = recupJSON["Data"].arrayObject {
//print("OK2")
self.ArrayFiches = resData as! [[String:AnyObject]] // Attribute json ==> ArrayFiches
}
if self.ArrayFiches.count > 0 {
self.tableFiches.reloadData()
}
}
当我想重新加载数据时,我使用:
tableFiches.startPullToRefresh()
它有效:)
关于ios - 如何在按钮操作中的 TableViewController 中重新加载 tableview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148740/