我有一个mainVC,我执行获取请求。 然后我将获取的数据传递给第二个视图控制器(HomeVc),然后传递给第三个(MyparamsVc) 在第三个VC我有一个tableView列出所有参数,它们与我在主VC中获取的实体有一对一的关系
在第四个View控制器中,我通过表单(addParamVC)添加新参数。保存后,我关闭控制器并弹出前一个(MyparamsVc)。 我的问题是tableview没有用新数据更新,除非我回到我的HomeVC然后再回到MyparamsVC
我已经实现了func controllerDidChange一个对象,但由于我没有在同一个控制器中执行提取,因此永远不会调用该函数...如何使用新数据更新tableview?
MainVc fetchRequest:
var controller: NSFetchedResultsController<SwimminPool>!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
attemptFetch()
}
func attemptFetch() {
let fetchRequest: NSFetchRequest<SwimminPool> = SwimminPool.fetchRequest()
let dataSort = NSSortDescriptor(key: "poolCreatedAt", ascending: false)
fetchRequest.sortDescriptors = [dataSort]
let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
controller.delegate = self
self.controller = controller
do {
try controller.performFetch()
} catch {
let error = error as NSError
print("\(error.debugDescription)")
}
}
addParamVC saving func:
var swimmingPoolToConnect: SwimmingPool!
var parameterToEdit: Parameter?
@IBAction func saveBtnPressed(_ sender: Any) {
var parameter: Parameter
if parameterToEdit == nil {
parameter = Parameter(context:context)
} else {
parameter = parameterToEdit!
}
if let pH = phTextField.text?.characters.split(separator: ",").joined(separator: ".") {
if let pHnoComma = Double(String(pH)) {
parameter.paramPH = pHnoComma
}
}
parameter.createdAt = Date()
swimmingPoolToConnect.addToParameters(parameter)
ad.saveContext()
self.presentingViewController?.dismiss(animated: true, completion: nil)
}
MyparamsVC:
var parameters = [Parameter]()
var swimmingPool: SwimmingPool! {
didSet {
parameters = (swimmingPool.parameters?.allObjects as! [Parameter]).sorted(by: {$0.createdAt! > $1.createdAt!})
}
}
var controller: NSFetchedResultsController<Parameter>?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ParamCell", for: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath)
return cell
}
func configureCell(cell: ParamCell, indexPath: IndexPath) {
let param = parameters[indexPath.row]
cell.configureCell(parameter: param)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let parameterToPass = parameters[indexPath.row]
DispatchQueue.main.async { () -> Void in
self.performSegue(withIdentifier: "MyParameterDetail", sender: parameterToPass)
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parameters.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
//Never enter these func
func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.beginUpdates()
}
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
tableView.endUpdates()
}
func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
switch(type){
case.insert:
if let indexPath = newIndexPath{
tableView.insertRows(at: [indexPath], with: .fade)
}
case.delete:
if let indexPath = indexPath{
tableView.deleteRows(at: [indexPath], with: .fade)
}
case.update:
if let indexPath = indexPath{
let cell = tableView.cellForRow(at: indexPath) as! ParamCell
configureCell(cell: cell, indexPath: indexPath)
}
case.move:
if let indexPath = indexPath{
tableView.deleteRows(at: [indexPath], with: .fade)
}
if let indexPath = newIndexPath{
tableView.insertRows(at: [indexPath], with: .fade)
}
}
}
答案 0 :(得分:1)
由于您有对呈现视图控制器的引用,请将该项添加到parameters
数组,并在MyparamsVC
addParamVC
中插入一行
ad.saveContext()
let paramsVC = self.presentingViewController as! MyparamsVC
let lastRow = paramsVC.parameters.count
paramsVC.parameters.append(parameter)
paramsVC.tableView.insertRows(at: [NSIndexPath(row: lastRow, section: 0)], with: .none)
paramsVC.dismiss(animated: true, completion: nil)
甚至在dismiss
方法
ad.saveContext()
let paramsVC = self.presentingViewController as! MyparamsVC
paramsVC.dismiss(animated: true) {
let lastRow = paramsVC.parameters.count
paramsVC.parameters.append(parameter)
paramsVC.tableView.insertRows(at: [NSIndexPath(row: lastRow, section: 0)], with: .none)
}
答案 1 :(得分:0)
您可以在保存到核心数据(如
)后刷新视图控制器add_library(foo INTERFACE)
find_package(foo ${FOO_VER})
if(NOT foo_FOUND)
include(ExternalProject)
include(GNUInstallDirs)
ExternalProject_Add(FOO_EXTERNAL
SOURCE_DIR "${FOO_SOURCE_DIR}"
BINARY_DIR "${FOO_BINARY_DIR}"
INSTALL_DIR "${FOO_INSTALL_DIR}"
CMAKE_ARGS "-DCMAKE_C_FLAGS=${CMAKE_C_FLAGS}"
"-DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DCMAKE_INSTALL_PREFIX=${FOO_INSTALL_DIR}"
)
add_dependencies(foo FOO_EXTERNAL)
set(foo_LIBRARY
"${FOO_INSTALL_DIR}/${CMAKE_INSTALL_LIBDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}foo${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(foo_INCLUDE_DIR "${FOO_INSTALL_DIR}/include")
endif()
target_link_libraries(foo INTERFACE ${foo_LIBRARY})
target_include_directories(foo INTERFACE ${foo_INCLUDE_DIR})
或者您可以像
一样重新加载tableview self.viewDidLoad()
self.viewWillAppear(true)