Change the table content in one viewController

I have a four buttons and one table view in one viewController. I want to select the entries in my table with the button. E.g. I have a list of persons and one button says "family" one "friends" one "collegues" and one "unknown" by clicking on one button the table only should show the persongroup I selected. The entries of the table view are stored in an global array. Changing the array with clicking is possible (I see this in the command line), but the table does not change. Does any one know why ? For storing and selecting data I use core data. In the IBAction I do this:
Code Block @IBAction func friendTapped(_ sender: Any) {
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else{
            return
        }
        let context = appDelegate.persistentContainer.viewContext
        let entityName = "Person"
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
        let pred = NSPredicate(format: "relationship_status CONTAINS 'friend'")
        request.predicate = pred
        do {
            let results = try context.fetch(request)
            for r in results{
                if let result = r as? NSManagedObject{
                    guard let relationship_status = result.value(forKey: "relationship_status") as? String else {
                        return
                    }
                    guard let name = result.value(forKey: "name") as? String else {
                        return
                    }
                    self.table_entries.append(name)
                    print("load")
                }
            }
            print ("done")
            print (table_entries)
        } catch  {
            print(error)
        }


Answered by Claude31 in 652216022
Appending to the dataSOurce is correct.
But you need to reload table after this.
It is easier to read if you paste code with Paste and Match Style:

Code Block
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else{
return
}
let context = appDelegate.persistentContainer.viewContext
let entityName = "Person"
let request = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
let pred = NSPredicate(format: "relationship_status CONTAINS 'friend'")
request.predicate = pred
do {
let results = try context.fetch(request)
for r in results{
if let result = r as? NSManagedObject{
guard let relationship_status = result.value(forKey: "relationship_status") as? String else {
return
}
guard let name = result.value(forKey: "name") as? String else {
return
}
self.table_entries.append(name)
print("load")
}
}
print ("done")
print (table_entries)
} catch {
print(error)
}


I do not see in this code anything trying to change the selection in the table. Am I missing something ?
What is table_entries ? Is it the data source ?
But then, why appending something ?

Note : in Swift, convention is not to use separators for naming, but camelCase: tableEntries
Yes table_entries ist the data source. I appending for each loaded data set the name, because I do not know how to get to the result(s) afterwards.

Accepted Answer
Appending to the dataSOurce is correct.
But you need to reload table after this.
Okay thank you ! But how I reload the table ? Is there a function for it ?
Change the table content in one viewController
 
 
Q