【问题标题】:View Controller will not appear when Table View Controller Cell is Selected选择 Table View Controller Cell 时不会出现 View Controller
【发布时间】:2021-02-18 14:31:03
【问题描述】:

我对 Swift/Xcode 比较陌生,我试图让视图控制器在选择按钮(添加按钮)或选择表格视图单元格(允许它们编辑)时显示不同的值。但是,我无法在选择我的行时得到任何响应,包括我已放入用于测试的打印语句,尽管当我将转换链接到按钮时,segue 确实起作用。该代码与我之前编写的使当前表视图出现的功能表视图单元格的代码相同。这可能是故事板的问题吗?

Storyboard screenshot with cell settings

这是我的代码:

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!]
        }
    }
}

我也查看了这个论坛帖子,因为它与我的相似,但我无法应用任何建议的修复。 https://developer.apple.com/forums/thread/14842

【问题讨论】:

  • 如果你使用 didselectRow 你需要使用 performSegueWithIdentifier
  • 您是否尝试过为单元格实现委托高度?

标签: ios swift xcode uistoryboard


【解决方案1】:

首先didSelectRow方法是错误的。代码是 Swift 2 代码。正确的签名是

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

你不知道为什么编译器不抱怨缺少override吗?

而不是从视图控制器重新连接segue(为什么有2个?)从表视图cell到目标控制器并删除didSelectRow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2012-07-17
    • 1970-01-01
    • 2013-04-21
    相关资源
    最近更新 更多