[英]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.