我正在尝试将nib文件加载到UITableView,我已经使用故事板将其放置在UIView中。
UITableView和UIView有两个独立的ViewControllers,我试图连接它们,但是我收到一条错误消息,指出无法识别的选择器。这是什么意思?
谢谢!
VC1
class CreateMeetPlanning: CreateMeet {
@IBOutlet var tableView: TableViewPlanning!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func closePlanning(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
//Automatically open keyboard
//override func viewDidAppear(_ animated: Bool) {
//super.viewDidAppear(animated)
//planningRow.becomeFirstResponder()
//}
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: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
VC2
class TableViewPlanning: UITableViewController, UITextFieldDelegate {
// forcing status bar to hide
override var prefersStatusBarHidden: Bool {
return true
}
// properties
var todo: Array<String> = [""]
var defTodo: Array<String> = [""]
// selecting textfield in first empty row
func responder() {
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .none, animated: false)
if let cell = tableView.cellForRow(at: IndexPath(row: 0, section: 0)) as? TableViewCell {
cell.textField.becomeFirstResponder()
}
}
// saving data to UserDefaults
func save() {
let defaults = UserDefaults.standard
defaults.set(self.todo, forKey: "todo")
}
// retrieving data from UserDefaults
func get() {
let defaults = UserDefaults.standard
self.todo = defaults.stringArray(forKey: "todo") ?? [""]
tableView.reloadData()
}
// action on tapping "Done" on a keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
let task = textField.text ?? " "
let indexPath = self.tableView.indexPathForView(textField)
if indexPath?.row == 0 {
self.todo.insert(task, at: 1)
print(self.todo)
tableView.beginUpdates()
self.tableView.insertRows(at: [indexPath!], with: .top)
tableView.endUpdates()
}
else {
self.todo[indexPath!.row] = task
tableView.reloadData()
}
responder()
save()
return true
}
// TABLEVIEW methods
// amount of rows
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.todo.count
}
// content for the row
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell
cell.textField.delegate = self
cell.textField.text = self.todo[indexPath.row].description
// cell.textField.becomeFirstResponder()
return cell
}
// deleting rows: all rows to be deleted if first row is deleted, others as usual.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath)
{
if indexPath.row != 0 {
if editingStyle == .delete
{
self.todo.remove(at: indexPath.row)
tableView.beginUpdates()
tableView.deleteRows(at: [indexPath], with: .automatic)
tableView.endUpdates()
}
}
else { if editingStyle == .delete
{
self.todo = self.defTodo
tableView.reloadData()
}
}
print(self.todo)
responder()
save()
}
@objc func refresh(refresh: UIRefreshControl) {
refresh.endRefreshing()
responder()
}
// ViewController life-cycle
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TableViewCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "Cell")
get()
refreshControl = UIRefreshControl()
refreshControl?.attributedTitle = NSAttributedString(string: "")
refreshControl?.addTarget(self, action: #selector(refresh(refresh:)), for: .valueChanged)
refreshControl?.tintColor = UIColor.red
tableView.refreshControl = refreshControl
responder()
}
override func viewDidAppear(_ animated: Bool) {
}
}
ERROR
2017-11-21 17:48:15.599430+0100 meetIOS[416:43942] [MC] Lazy loading NSBundle MobileCoreServices.framework 2017-11-21 17:48:15.601629+0100 meetIOS[416:43942] [MC] Loaded MobileCoreServices.framework 2017-11-21 17:48:15.631201+0100 meetIOS[416:43942] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2017-11-21 17:48:15.797665+0100 meetIOS[416:43942] refreshPreferences: HangTracerEnabled: 0 2017-11-21 17:48:15.797774+0100 meetIOS[416:43942] refreshPreferences: HangTracerDuration: 500 2017-11-21 17:48:15.797820+0100 meetIOS[416:43942] refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0 2017-11-21 17:48:17.744420+0100 meetIOS[416:43942] -[meetIOS.TableViewPlanning superview]: unrecognized selector sent to instance 0x101d1a9f0 2017-11-21 17:48:17.746086+0100 meetIOS[416:43942] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[meetIOS.TableViewPlanning superview]: unrecognized selector sent to instance 0x101d1a9f0' *** First throw call stack: (0x182bd5d04 0x181e24528 0x182be31c8 0x18c3cb110 0x182bdb6b0 0x182ac101c 0x18c014074 0x18c2dc5ec 0x18c747160 0x18c7472d8 0x18c746e98 0x18c2dc57c 0x18c747160 0x18c746e98 0x18c59c0cc 0x18c747160 0x18c7472d8 0x18c746e98 0x18c59b40c 0x18c37b17c 0x18c12aee4 0x18c00cba4 0x18c00cad4 0x18c38ac04 0x18c383a2c 0x18c385428 0x18c387ccc 0x18c388200 0x18c387c1c 0x18c0ebaa8 0x18c910038 0x18c92ab30 0x18c92a9d4 0x18c92aca8 0x18c041608 0x18c041588 0x18c02c2f0 0x18c040e7c 0x18c04099c 0x18c03be6c 0x18c00d378 0x18c95a85c 0x18c95cde8 0x18c955d04 0x182b7e2e8 0x182b7e268 0x182b7daf0 0x182b7b6c8 0x182a9bfb8 0x184933f84 0x18c0702e8 0x100948e14 0x1825be56c) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
答案 0 :(得分:0)
问题很可能出在您拥有CreateMeetPlanning
VC的故事板中,您有一个已分配给插座tableView: TableViewPlanning!
的tableView。但TableViewPlanning
不是UITableView,而是UITableViewController
。因此崩溃。
您需要在storybard中使用容器视图来嵌入UITabelviewcontroller,然后在将其连接到插座之前将其类更改为TableViewPlanning
。在answer中已经完成了类似的事情。