gpt4 book ai didi

swift - 什么时候调用 CellForRowAt? -- 排队直到 VC 排在最前面?

转载 作者:行者123 更新时间:2023-11-28 13:40:44 26 4
gpt4 key购买 nike

我有两个 View Controller ,A 和 B。

A 有一个 TableView 。

A 包裹在一个导航 Controller 中,允许它推送到 B。B 有一个调用 tableView.reloadData() 的处理程序。但是,在 B 弹出并且 A 再次可见之前,不会在 A 中调用 cellForRowAt 方法。

是否有正式文档说明“cellForRowAt”仅在 VC 是最顶层 View Controller 时才被调用?这是期望的结果,我想确保这是假设发生的。

-- denotes action
// denotes current VC
console denotes print message

//AVC:
--push to BVC
//BVC:
--press handler Button
console: "handling"
console: "nil"
console: "numberOfRowsInSection"
--press back navigation
console: "Disappearing"
//AVC:
console: "CellForROwAt"

复制文件要点

https://gist.github.com/MochaTheCoder/628af8950f39e5fc0896cc12d77c6fb2

-- 代码--

class AViewController: UIViewController {

@objc private func pushBVC() {
let bVC = BViewController()
bVC.handler = {
print("handling")
print(self.view.window)
self.tableView.reloadData()
}
navigationController?.pushViewController(bVC, animated: true)
}

}

extension AViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRowsInSection")
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("CellForROwAt")
return UITableViewCell()
}
}

----

class BViewController: UIViewController {

var handler: (() -> Void) = {}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
print("Disappearing")
}

@objc private func doHandler() {
handler()
}

}

最佳答案

当第二个 View Controller 被按下时,第一个 View Controller 实际上从窗口中移除。因此,在您调用 reloadData 时, TableView 不在窗口层次结构中,因此此时它不会自行更新。

在第二个 View Controller 被关闭并再次显示第一个 View Controller 的过程中,它被添加回窗口层次结构(连同 TableView ),并且 TableView 使用此事件刷新自身与挂起更新。

这就是为什么在 print("Disappearing") 消息之后您看不到对 cellForRowAt 的调用。

所以并不是表在不可见时不更新,而是在它不属于窗口层次结构时才不更新。

关于swift - 什么时候调用 CellForRowAt? -- 排队直到 VC 排在最前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56067663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com