简体   繁体   English

选择表视图控制器单元格时不会出现视图控制器

[英]View Controller will not appear when Table View Controller Cell is Selected

I am relatively new to Swift/Xcode and I am trying to make a view controller appear with different values when a button is selected (an add button) or a table view cell is selected (which allows them to edit).我对 Swift/Xcode 比较陌生,我试图在选择按钮(添加按钮)或选择表格视图单元格(允许它们进行编辑)时使视图控制器显示不同的值。 However, I am unable to get any response upon the selection of my row including a print statement I have put in for testing, although the segue does function when I link the transition to a button.但是,我无法在选择我的行时得到任何响应,包括我为测试而放入的打印语句,尽管当我将转换链接到按钮时,segue 确实起作用。 The code is identical to that of a functioning table view cell I wrote previously that makes the current table view appear.该代码与我之前编写的使当前表视图出现的功能表视图单元格的代码相同。 Could this be an issue with the storyboard?这可能是故事板的问题吗?

Storyboard screenshot with cell settings带有单元格设置的故事板屏幕截图

Here is my code:这是我的代码:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
    cell.textLabel?.text = items[indexPath.row].name
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("Selected")
}

//Sends current receipt information to the AddItemViewController display when tapped
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "AddItemViewSegue" || segue.identifier == "EditItemViewSegue" {
        let destination = segue.destination as? AddItemViewController
        destination?.currentReceipt = currentReceipt
        let index = tableView.indexPathForSelectedRow?.row
        print("test")
        if segue.identifier == "AddItemViewSegue" {
            destination?.addMessage = "Add a New Item to Your Receipt"
            destination?.buttonMessage = "Add Item"
        }
        else {
            print("test1")
            destination?.addMessage = "Edit an Item in Your Receipt"
            destination?.buttonMessage = "Edit Item"
            destination?.currentItem = items[index!]
        }
    }
}

I have also looked into this forum post because it is similar to mine but I was unable to apply any of the suggested fixes.我也查看了此论坛帖子,因为它与我的类似,但我无法应用任何建议的修复程序。 https://developer.apple.com/forums/thread/14842 https://developer.apple.com/forums/thread/14842

First of all the didSelectRow method is wrong.首先didSelectRow方法是错误的。 The code is Swift 2 code.代码是 Swift 2 代码。 The proper signature is正确的签名是

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { ...

Didn't you wonder why the compiler doesn't complain about the missing override ?难道你不知道为什么编译器不抱怨缺少override吗?

Rather than from the view controller reconnect the segue (why are there 2 ones?) from the table view cell to the destination controller and delete didSelectRow而不是从视图控制器重新连接segue(为什么有2个?)从表视图单元格到目标控制器并删除didSelectRow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表单元进入视图控制器 - Table cell into view controller 将多个选定表单元格中的数组传递到下一个视图控制器 - Pass the array in multiple selected table cell into next view controller 使用来自单独的表视图控制器的选定单元格填充文本字段 - Populating a Text Field with a Selected Cell from a Separate Table View Controller 将核心数据从选定的表格单元格传递到新的视图控制器 - Pass core data from selected table cell to new view controller 在容器视图中选择表格单元格时与父视图控制器进行交互 - Interact with Parent view controller when selecting a table cell in a container view 当按下表格单元格时,将视图加载到导航视图控制器中 - Load view into navigation view controller when table cell is pressed 表视图单元格到另一个视图控制器 - Table view cell to another view controller 问题以模态呈现具有搜索显示控制器的表视图控制器并返回所选单元格 - issue modally presenting a table view controller that has a search display controller and returning the selected cell 单击表格单元格时,将变量值发送到下一个视图控制器 - Send variable value to next view controller when click a table cell 拆分视图控制器折叠/展开时更新表单元格附件 - Updating Table Cell Accessory when Split View Controller Collapses/Expands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM