swift - 委托函数中的数组为空
问题描述
我刚刚实现了这个协议委托构造。但是什么都出错了。我按下自定义 UITableViewCell 中的删除按钮,我想删除数组中的一个项目。我到达了委托函数,我可以打印 id。但是数组是空的(nil)。什么地方出了错?
protocol KonfigDataDelegate: class {
func deleteItem(id: UUID)
}
class MyClass {
var Bezeichnung: Sting = ""
...
}
class ViewControllerKonfiguration: UIViewController, UITableViewDelegate, UITableViewDataSource, KonfigDataDelegate {
var myArray: [MyClass] = []
override func viewDidLoad() {
super.viewDidLoad()
...
myArray = loadAnyDataInArray()
print(myArray.count) --> 3 elements in array
...
}
func deleteItem(id: UUID) {
print("ID: \(id)") --> prints my id
print(myArray.count) --> 0 - Why is my array empty?
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell: UITableViewCell
cell = tableView.dequeueReusableCell(withIdentifier: "tvcMahlzeit", for: indexPath) as! TVCKonfigMahlzeit
(cell as! TVCKonfigMahlzeit).edBezeichnung.text = myArray[indexPath.row]
(cell as! TVCKonfigMahlzeit).delegate = self
}
}
class TVCKonfigMahlzeit: UITableViewCell {
var id: UUID?
weak var delegate: KonfigDataDelegate?
@IBAction func buttonDeletePresed(_ sender: Any) {
var vc = ViewControllerKonfiguration()
self.delegate = vc as KonfigDataDelegate
delegate?.deleteItem(id: id!)
}
}
解决方案
当我删除这些行时,它可以工作。我在这里创建了一个新的 ViewControllerKonfiguration 实例,那里的数组是空的。
@IBAction func buttonDeletePresed(_ sender: Any) {
remove this ––> var vc = ViewControllerKonfiguration()
remove this ––> self.delegate = vc as KonfigDataDelegate
delegate?.deleteItem(id: id!)
}
推荐阅读
- reactjs - 连接的容器组件在树的下方?
- c# - 如何将网页中显示的文本作为字符串
- google-apps-script - 使用 Google Apps 脚本响应 Google Calendar API 的推送通知
- javascript - 是否可以在 Javascript 中以编程方式更改词法范围?
- android - 如何从外部应用程序获取数据?
- python - 当我从另一个点再次调用它时,我的函数中的 Python 循环不再运行
- jquery - ajax xhr.getResponseHeader - http 错误响应中缺少自定义标头
- python - 在保留 Tk 对象时关闭 cmd
- r - 计算 R 中的总观察天数(lubridate + dplyr)
- spring - RestTemplate 与 WebClient 在基于 Servlet 的 web-mvc 应用程序中的优势