У меня есть ViewController (CreateRowController), у которого есть текстовое поле для строки, которое затем передается MasterViewController для создания строки. Но новая строка не добавлена. Я думал, что это как-то связано с обновлениями табличного представления, поэтому я попытался сделать reloadData () вместо beginUpdates () и endUpdates (), но это не помогло. Я также пробовал разные tableView.insertRows, но это ничего не изменило.
MasterViewController:
import UIKit
var className: String!
class MasterViewController: UITableViewController {
var detailViewController: DetailViewController? = nil
var objects = [] as Array
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
navigationItem.leftBarButtonItem = editButtonItem
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(createNewRow(_:)))
navigationItem.rightBarButtonItem = addButton
if let split = splitViewController {
let controllers = split.viewControllers
detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
}
}
override func viewWillAppear(_ animated: Bool) {
//clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
}
@objc
func createNewRow (_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "createRowController") as! CreateRowController
self.present(newViewController, animated: true, completion: nil)
}
func insertNewObject (_sender: Any) {
print(className!)
tableView.beginUpdates()
objects.append(className!) //can change className
tableView.insertRows(at: [IndexPath(row: objects.count-1, section: 0)], with: .automatic)
tableView.endUpdates()
}
// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let indexPath = tableView.indexPathForSelectedRow {
let object = objects[indexPath.row] as! String
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailItem = object
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}
// MARK: - Table View
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objects.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let object = objects[indexPath.row] as! String
cell.textLabel!.text = object.description
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
objects.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
}
CreateViewController:
import UIKit
class CreateRowController: UIViewController {
@IBOutlet weak var nameOfEvent: UITextField!
@IBOutlet weak var numberOfClasses: UITextField!
var name = ""
var numClasses = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func create(_ sender: Any) {
self.name = nameOfEvent.text!
self.numClasses = Int(numberOfClasses.text!)!
className = self.name
MasterViewController().insertNewObject(_sender: self)
performSegue(withIdentifier: "createdRowToList", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is MasterViewController {
className = self.name
}
}
}
Может кто-нибудь помочь мне с этим? Извините, я довольно новичок в xcode.
Редактировать: я попробовал ответ Вадиана, поэтому посмотрите на комментарий к его ответу, если вам нужна дополнительная информация