我正在创建一个应用程序,用于加载用户可以添加到表格中的书籍和书籍类别列表。我创建了一个结构来组织数据。当用户想要添加新书或类别时,它会显示我的AddBookCategoryViewController
。当我尝试添加类别时,它会成功将类别加载为标题标题。但是,当我尝试添加第二个类别时,它不会第二次添加它。它不会带来任何错误,它会通过整个prepareForSegue
。我完全不知道如何解决这个问题。
注意:我没有尝试一次添加两个类别,我试图成功添加一个(我可以),然后点击添加按钮尝试添加第二个。
感谢。
以下是ViewController
的代码:
class ViewController: UITableViewController {
var testArray = [Category]()
var sections: [Category] = CategoryData().getCategoryFromData()
@IBAction func editButton(_ sender: UIBarButtonItem) {
self.isEditing = !self.isEditing
}
@IBAction func addButton(_ sender: UIBarButtonItem) {
}
@IBAction func saveNewData(_ segue: UIStoryboardSegue) {
if segue.sourceViewController is AddBookCategoryViewController {
print("saveNewData")
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func numberOfSections(in tableView: UITableView) -> Int {
return testArray.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return testArray[section].sectionItems!.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bookList", for: indexPath)
cell.textLabel?.text = testArray[indexPath.section].sectionItems?[indexPath.row]
cell.detailTextLabel?.text = ""
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(sections[indexPath.section].sectionItems?[indexPath.row])
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return testArray[section].sectionHeading
}
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as? UITableViewHeaderFooterView
header?.textLabel?.textColor = UIColor.orange()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这是AddBookCategoryViewController:
import UIKit
class AddBookCategoryViewController: UIViewController {
var data: Category?
@IBOutlet weak var segmentedControl: UISegmentedControl!
@IBAction func cancelButton(_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var pickCategory: UIButton!
@IBAction func segmentedChangeFields(_ sender: AnyObject) {
if segmentedControl.selectedSegmentIndex == 0 {
nameLabel.text = "Name of Book"
authorLabel.text = "Author"
authorLabel.isHidden = false
authorTextField.isHidden = false
pickCategory.isHidden = false
}
else {
nameLabel.text = "Name of Category"
authorLabel.isHidden = true
authorTextField.isHidden = true
pickCategory.isHidden = true
categoryLabel.isHidden = true
}
}
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var authorTextField: UITextField!
@IBOutlet weak var authorLabel: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "saveNewData" {
let vc = segue.destinationViewController as! ViewController
print("1")
if segmentedControl.selectedSegmentIndex == 0 {
print("2")
} else {
print("3")
vc.testArray.append(Category(sectionHeading: nameTextField.text!, sectionItems: []))
}
}
print("4")
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
答案 0 :(得分:0)
作为参考,我设法弄清了问题。我能够将tableView.reloadData()
添加到View Controller中的@IBAction func saveNewData
,就像这样。
@IBAction func saveNewData(_ segue: UIStoryboardSegue) {
if segue.sourceViewController is AddBookCategoryViewController {
print("saveNewData")
tableView.reloadData()
}
}
感谢。