模态视图关闭后重新加载 table 视图
reload table view after modal view dismissal
我看了很多关于这个主题的其他问题。但它们都导致同样的事情 "view did load" "view did appear" 和 "view will appear" 我已经把
tableview.reloadData()
在所有这些中,但在关闭模态视图后它仍然不会重新加载,直到我终止应用程序然后我再次 运行。我已经看到您可以通过通知中心执行此操作并且已经看到 Here 并且我尝试了该方法但它似乎对我不起作用。
提前感谢您的帮助!!!
import UIKit
class mainScreen: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// MARK: - Table View Setup
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if allPeople.count == theAssignments.count {
return allPeople.count
}else {
print("else in number of rows in section BAD")
return allPeople.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleTVCell", for: indexPath)
cell.textLabel?.text = theAssignments[indexPath.row]
cell.detailTextLabel?.text = allPeople[indexPath.row]
return cell
}
// MARK: - System setup
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableview.allowsSelection = false
/////////here
tableview.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
////////here
tableview.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
let NamesObject = UserDefaults.standard.object(forKey: "Name" + whatGroup)
if let tempItems = NamesObject as? [String] {
allPeople = tempItems
print("saved data in main (Name)")
}
let AssignmentObject = UserDefaults.standard.object(forKey: whatGroup + "Assignment")
if let tempItems = AssignmentObject as? [String] {
theAssignments = tempItems
print("saved data in main (Assignments)")
}
////////here
tableview.reloadData()
}
// MARK: - the Exit Segue
@IBAction func exitSegue(segue: UIStoryboardSegue) {
/////////here
tableview.reloadData()
isAddingName = 2
print("Exited to main")
}
}
这是我要添加东西的地方
@IBAction func saveButton(_ sender: UIButton) {
if isInMain == true {
if IsAddingAssignment == true {
// MARK: Add Assignment
let AssignmentObject = UserDefaults.standard.object(forKey: whatGroup + "Assignment") as? [String]
var Assignment: [String]
if let tempItems = AssignmentObject {
Assignment = tempItems
Assignment.append("\(EnteredObjectTextFeild.text!)")
print(Assignment)
} else {
Assignment = [EnteredObjectTextFeild.text!]
print("user defaults error 2")
}
UserDefaults.standard.set(Assignment, forKey: whatGroup + "Assignment")
EnteredObjectTextFeild.text = ""
}else {
// MARK: Add Name
if AllNames.contains("\(EnteredObjectTextFeild.text!)"){
if namesForEachGroup.contains(EnteredObjectTextFeild.text!) {
EnteredObjectTextFeild.text = ""
AlertAction(Title: "Name Exists", Message: "This name already exists in this group", alerTitle: "OK")
}else {
addNameToGroup()
EnteredObjectTextFeild.text = ""
}
}else {
let NameObject = UserDefaults.standard.object(forKey: "Name") as? [String]
var Name: [String]
if let tempItems = NameObject {
Name = tempItems
Name.append("\(EnteredObjectTextFeild.text!)")
print(Name)
} else {
Name = [EnteredObjectTextFeild.text!]
print("user defaults error 3")
}
UserDefaults.standard.set(Name, forKey: "Name")
addNameToGroup()
EnteredObjectTextFeild.text = ""
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
}
}else if isInMain == false {
// MARK: Add Group
if allGroups.contains("\(EnteredObjectTextFeild.text!)"){
EnteredObjectTextFeild.text = ""
AlertAction(Title: "Group Exists", Message: "This Group already exists", alerTitle: "OK")
}else {
let groupsObject = UserDefaults.standard.object(forKey: "Groups") as? [String]
var groups: [String]
if let tempItems = groupsObject {
groups = tempItems
groups.append(EnteredObjectTextFeild.text!)
print(groups)
} else {
groups = [EnteredObjectTextFeild.text!]
print("Bad")
}
UserDefaults.standard.set(groups, forKey: "Groups")
EnteredObjectTextFeild.text = ""
}
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
// MARK: Add Name func
func addNameToGroup(){
let nameObject = UserDefaults.standard.object(forKey: "Name" + whatGroup) as? [String]
var NameForGroup: [String]
if let tempItems = nameObject {
NameForGroup = tempItems
NameForGroup.append("\(EnteredObjectTextFeild.text!)")
print(NameForGroup)
} else {
NameForGroup = [EnteredObjectTextFeild.text!]
print("user defaults error 4")
}
UserDefaults.standard.set(NameForGroup, forKey: "Name" + whatGroup)
namesForEachGroup = NameForGroup
print("saved to group")
EnteredObjectTextFeild.text = ""
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
这是我回到第一个 VC
的地方
@IBAction func BackPressed(_ sender: UIButton) {
if isInMain == true {
performSegue(withIdentifier: "FromAddToMain", sender: UIButton())
}else {
performSegue(withIdentifier: "FromAddToGroup", sender: UIButton())
}
}
我将此代码添加到每个退出 segue
theAssignments = UserDefaults.standard.array(forKey: whatGroup + "Assignments") as! [String]
最简单的解决方案是如何在 undwin-perform 中传递数据,
配置是;
在父控制器中创建一个函数,它将成为接收执行焦点的函数:
@IBAction func close (segue: UIStoryboard) {
tableView.reload ()
}
然后在子控制器中创建 undwin segue(向后)分配先前创建的关闭函数,并在输出函数中执行该行。
performsegue (whithIdentifier: "undwinSegueclose", sender: self)
我看了很多关于这个主题的其他问题。但它们都导致同样的事情 "view did load" "view did appear" 和 "view will appear" 我已经把
tableview.reloadData()
在所有这些中,但在关闭模态视图后它仍然不会重新加载,直到我终止应用程序然后我再次 运行。我已经看到您可以通过通知中心执行此操作并且已经看到 Here 并且我尝试了该方法但它似乎对我不起作用。 提前感谢您的帮助!!!
import UIKit
class mainScreen: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableview: UITableView!
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
// MARK: - Table View Setup
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if allPeople.count == theAssignments.count {
return allPeople.count
}else {
print("else in number of rows in section BAD")
return allPeople.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PeopleTVCell", for: indexPath)
cell.textLabel?.text = theAssignments[indexPath.row]
cell.detailTextLabel?.text = allPeople[indexPath.row]
return cell
}
// MARK: - System setup
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tableview.allowsSelection = false
/////////here
tableview.reloadData()
}
override func viewWillAppear(_ animated: Bool) {
////////here
tableview.reloadData()
}
override func viewDidAppear(_ animated: Bool) {
let NamesObject = UserDefaults.standard.object(forKey: "Name" + whatGroup)
if let tempItems = NamesObject as? [String] {
allPeople = tempItems
print("saved data in main (Name)")
}
let AssignmentObject = UserDefaults.standard.object(forKey: whatGroup + "Assignment")
if let tempItems = AssignmentObject as? [String] {
theAssignments = tempItems
print("saved data in main (Assignments)")
}
////////here
tableview.reloadData()
}
// MARK: - the Exit Segue
@IBAction func exitSegue(segue: UIStoryboardSegue) {
/////////here
tableview.reloadData()
isAddingName = 2
print("Exited to main")
}
}
这是我要添加东西的地方
@IBAction func saveButton(_ sender: UIButton) {
if isInMain == true {
if IsAddingAssignment == true {
// MARK: Add Assignment
let AssignmentObject = UserDefaults.standard.object(forKey: whatGroup + "Assignment") as? [String]
var Assignment: [String]
if let tempItems = AssignmentObject {
Assignment = tempItems
Assignment.append("\(EnteredObjectTextFeild.text!)")
print(Assignment)
} else {
Assignment = [EnteredObjectTextFeild.text!]
print("user defaults error 2")
}
UserDefaults.standard.set(Assignment, forKey: whatGroup + "Assignment")
EnteredObjectTextFeild.text = ""
}else {
// MARK: Add Name
if AllNames.contains("\(EnteredObjectTextFeild.text!)"){
if namesForEachGroup.contains(EnteredObjectTextFeild.text!) {
EnteredObjectTextFeild.text = ""
AlertAction(Title: "Name Exists", Message: "This name already exists in this group", alerTitle: "OK")
}else {
addNameToGroup()
EnteredObjectTextFeild.text = ""
}
}else {
let NameObject = UserDefaults.standard.object(forKey: "Name") as? [String]
var Name: [String]
if let tempItems = NameObject {
Name = tempItems
Name.append("\(EnteredObjectTextFeild.text!)")
print(Name)
} else {
Name = [EnteredObjectTextFeild.text!]
print("user defaults error 3")
}
UserDefaults.standard.set(Name, forKey: "Name")
addNameToGroup()
EnteredObjectTextFeild.text = ""
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
}
}else if isInMain == false {
// MARK: Add Group
if allGroups.contains("\(EnteredObjectTextFeild.text!)"){
EnteredObjectTextFeild.text = ""
AlertAction(Title: "Group Exists", Message: "This Group already exists", alerTitle: "OK")
}else {
let groupsObject = UserDefaults.standard.object(forKey: "Groups") as? [String]
var groups: [String]
if let tempItems = groupsObject {
groups = tempItems
groups.append(EnteredObjectTextFeild.text!)
print(groups)
} else {
groups = [EnteredObjectTextFeild.text!]
print("Bad")
}
UserDefaults.standard.set(groups, forKey: "Groups")
EnteredObjectTextFeild.text = ""
}
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
// MARK: Add Name func
func addNameToGroup(){
let nameObject = UserDefaults.standard.object(forKey: "Name" + whatGroup) as? [String]
var NameForGroup: [String]
if let tempItems = nameObject {
NameForGroup = tempItems
NameForGroup.append("\(EnteredObjectTextFeild.text!)")
print(NameForGroup)
} else {
NameForGroup = [EnteredObjectTextFeild.text!]
print("user defaults error 4")
}
UserDefaults.standard.set(NameForGroup, forKey: "Name" + whatGroup)
namesForEachGroup = NameForGroup
print("saved to group")
EnteredObjectTextFeild.text = ""
NamePicker.reloadAllComponents()
AsignmentPicker.reloadAllComponents()
}
这是我回到第一个 VC
的地方@IBAction func BackPressed(_ sender: UIButton) {
if isInMain == true {
performSegue(withIdentifier: "FromAddToMain", sender: UIButton())
}else {
performSegue(withIdentifier: "FromAddToGroup", sender: UIButton())
}
}
我将此代码添加到每个退出 segue
theAssignments = UserDefaults.standard.array(forKey: whatGroup + "Assignments") as! [String]
最简单的解决方案是如何在 undwin-perform 中传递数据, 配置是; 在父控制器中创建一个函数,它将成为接收执行焦点的函数:
@IBAction func close (segue: UIStoryboard) {
tableView.reload ()
}
然后在子控制器中创建 undwin segue(向后)分配先前创建的关闭函数,并在输出函数中执行该行。
performsegue (whithIdentifier: "undwinSegueclose", sender: self)