【发布时间】:2020-05-28 17:23:27
【问题描述】:
我正在尝试设置一个静态表视图,该视图与另一个带有列表的表视图有segue。然后用户从列表中选择一个项目,并更新主(静态)表视图。我有设置代表,但我认为我遗漏了一些东西。从列表中选择一个项目时,它会引发“强制展开可选时意外找到'nil'”的异常。看来可选的是协议本身。
主表视图文件
//
// 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
}
}
【问题讨论】:
-
您必须实现(取消注释)
prepare(for segue并在其中将委托设置为self。并且永远不要将表示委托对象的属性声明为隐式展开的可选。将其声明为常规可选和可选链调用。 -
@vadian,你会为“prepare(for segue”) 块输入什么?我尝试了很多不同的变体,但似乎无法让它正常工作。
标签: ios swift xcode tableview swift-protocols