Я пытаюсь настроить статическое c табличное представление, которое имеет переход к другому табличному представлению со списком. Затем пользователь выбирает элемент из списка, и он обновляет основное (stati c) табличное представление. У меня есть делегаты по настройке, но я думаю, что мне что-то не хватает. При выборе элемента из списка выдается исключение «неожиданное обнаружение нуля при принудительном развертывании по желанию». Похоже, необязательным является сам протокол.
Снимок экрана с ошибкой
Схема раскадровки
Основной файл табличного представления
//
// AddAssetTableViewController.swift
// ItemizePro
//
// Created by Tyler Wasick on 5/27/20.
// Copyright © 2020 Tyler Wasick. All rights reserved.
//
import UIKit
class AddAssetTableViewController: UITableViewController {
// TODO: Add image IBOutlet
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var descriptionTextField: UITextField!
@IBOutlet weak var datePurchasedTextField: UITextField!
@IBOutlet weak var manufactureTextField: UITextField!
@IBOutlet weak var modelTextField: UITextField!
@IBOutlet weak var serialNumberTextField: UITextField!
@IBOutlet weak var costTextField: UITextField!
@IBOutlet weak var roomTextField: UITextField!
@IBOutlet weak var notesTextView: UITextView!
@IBOutlet weak var receiptTextField: UITextField!
@IBOutlet weak var addUIButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
}
// MARK: - Table view data source
/*
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath)
// Configure the cell...
return cell
}
*/
/*
// Override to support conditional editing of the table view.
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 to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
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
}
}
*/
/*
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
*/
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
/*
// 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: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
// MARK: - Functions
// MARK: - IBAction outlets
@IBAction func addButtonTapped(_ sender: UIButton) {
}
}
extension AddAssetTableViewController : RoomDelegate {
func tappedOnRoom(room: String) {
roomTextField.text = room
}
}
Табличное представление со списком элементов (комнат) для выбора, которое передается обратно в первый контроллер табличного представления
//
// RoomListViewController.swift
// ItemizePro
//
// Created by Tyler Wasick on 5/25/20.
// Copyright © 2020 Tyler Wasick. All rights reserved.
//
import UIKit
protocol RoomDelegate {
func tappedOnRoom(room: String)
}
class RoomListViewController : UIViewController {
var selectionDelegate : RoomDelegate!
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// Configure table view
tableView.dataSource = self
tableView.delegate = self
}
// When user taps on a room, it returns the room to the delagate and dismisses
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Assign the selection to 'selectedRoom'
let selectedRoom = UserSettings.roomList[indexPath.row]
// Return the room to the delegate
selectionDelegate.tappedOnRoom(room: selectedRoom)
}
}
extension RoomListViewController: UITableViewDataSource, UITableViewDelegate {
// UITableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return UserSettings.roomList.count
}
// UITableViewDataSource
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Setup cell
let cell = tableView.dequeueReusableCell(withIdentifier: "RoomListCell", for: indexPath) as! RoomListView
// Set the details for the cell
let room = UserSettings.roomList[indexPath.row]
cell.setRoomCell(room)
// Return the cell
return cell
}
}