简体   繁体   English

添加新值时,如何从另一个viewController重新加载tableview?

[英]How can I reload the tableview from another viewController when I add a new value?

I already tried the answers here at StackOverFlow with the same question as mine, but it didn't work. 我已经在StackOverFlow尝试了与我相同的问题,但没有成功。

Here is my story board. 这是我的故事板。

Story Board 故事板

Here is the code for my ViewController 这是我的ViewController的代码

var people = [Person]()

@IBOutlet weak var patientView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let fetchReq: NSFetchRequest<Person> = Person.fetchRequest()

    do{
        let people = try PersistenceService.context.fetch(fetchReq)
        self.people = people
        self.patientView.reloadData()
    }catch{

    }
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    patientView.reloadData()
}

and here is the code of my TableViewController 这是我的TableViewController的代码

var people = [Person]()

@IBOutlet weak var patientName: UITextField!
@IBOutlet weak var patientAge: UITextField!

@IBAction func btnAdd(_ sender: UIButton) {

    let person = Person(context: PersistenceService.context)
    person.name = patientName.text
    person.age = Int16(patientAge.text!)!
    PersistenceService.saveContext()
    self.people.append(person)

}

@IBAction func btnClear(_ sender: UIButton) {

}

override func viewDidLoad() {
    super.viewDidLoad()
}

The problem is that when I add a person, and go back to the main ViewController, the table won't reload. 问题是,当我添加一个人并返回到主ViewController时,该表将不会重新加载。 However, when I close the app and open again the data will be fetch and reloaded. 但是,当我关闭应用程序并再次打开时,数据将被提取并重新加载。

Try this. 尝试这个。 Put the fetch request in the viewWillAppear and remove it from the viewDidLoad. 将获取请求放入viewWillAppear中,并将其从viewDidLoad中删除。 Tell me if it works. 告诉我是否可行。

 override func viewWillAppear(_ animated: Bool) {
        let fetchReq: NSFetchRequest<Person> = Person.fetchRequest()

        do{
            let people = try PersistenceService.context.fetch(fetchReq)
            self.people = people
            self.patientView.reloadData()
        }catch{

        }
         patientView.reloadData()
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用tableview重新加载以下viewcontroller? - How can I reload the following viewcontroller with a tableview? 我如何从AppDelegate访问另一个ViewController中的Tableview的ViewController? - How can i access a ViewController that's a segue from a Tableview in another ViewController from my AppDelegate? 如何将tableView的多行传输到另一个ViewController - How can I transfer multiple rows of a tableView to another ViewController 从另一个ViewController Swift 4.2重新加载tableView - reload tableView from Another ViewController Swift 4.2 如何在当前viewcontroller中重新加载另一个uiviewcontroller的tableview - how to reload tableview of another uiviewcontroller in current viewcontroller 当我使用2个表视图时,如何将数据(托管对象)从一个表视图移动到另一表视图? - How can I move data(managedobject) from one tableview to another tableview when I used 2 tableviews? 如何从类别表重新加载RootViewController中列出的tableView? - How can I reload the tableView that listed in RootViewController from a category table? iOS Swift如何从其他控制器重新加载tableView - IOS Swift how can I reload a tableView from a different controller 在tableView中滚动时,如何隐藏collectionView(知道此ViewCOntroller是tableview的一半,collectionview的一半) - How can I hide a collectionView when scrolling in my tableView (knowing that this ViewCOntroller is half tableview and half collectionview) 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM