【发布时间】: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