我正在尝试将笔尖文件加载到UITableView中,该文件已使用情节提要板放置在UIView中。

UITableView和UIView有两个单独的ViewController,我正在尝试将它们连接起来,但是却收到一条错误消息,提示无法识别的选择器。这是什么意思?

谢谢!

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) {
}
}


错误

2017-11-21 17:48:15.599430 + 0100 meetIOS [416:43942] [MC]延迟加载NSBundle MobileCoreServices.framework
2017-11-21 17:48:15.601629 + 0100 meetIOS [416:43942] [MC]已加载MobileCoreServices.framework
2017-11-21 17:48:15.631201 + 0100 meetIOS [416:43942] [MC] systemgroup.com.apple.configurationprofiles路径的系统组容器为/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:HangTracer持续时间: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]:无法识别的选择器已发送到实例0x101d1a9f0
2017-11-21 17:48:17.746086 + 0100 meetIOS [416:43942] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[meetIOS.TableViewPlanning superview]:无法识别的选择器已发送至实例0x101d1a9f0'
***首先抛出调用堆栈:
(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:以NSException类型的未捕获异常终止
(lldb)

最佳答案

问题很可能是在您拥有CreateMeetPlanning VC的情节提要中,您有一个分配给出口tableView: TableViewPlanning!的tableView。但是TableViewPlanning不是UITableView,而是UITableViewController。因此崩溃。

您需要在Storybard中使用容器视图来嵌入UITabelviewcontroller,然后将其类更改为TableViewPlanning,然后再将其连接到插座。在此answer中已经完成了类似的操作。

10-08 06:07