swift - 视图第二次出现后,Swift UITableView cellForRowAt indexPath 不会在 tableView.reloadData() 上被调用
问题描述
我的问题的总结是;当在tableView.reloadData()
第一次加载视图时调用firestore的本地更改时,它会按预期工作并更新。但是,在我与其他 viewController 来回切换之后,虽然最初在viewDidAppear()
tableView 上重新加载了,但在本地更改后它不再这样做。
我已经包含了我的项目的一个更简单的版本,以更好地解释并使其可重现;
类项目
let db = Firestore.firestore()
static var list = [String:[String:Any]]()
static var listenerSet = Bool()
static var listener: ListenerRegistration!
func setListener(completion: @escaping (String) -> Void) {
if !Item.listenerSet {
print("Attaching item document listener.")
Item.listener = db.collection("Items").document("default").addSnapshotListener({ (document, error) in
let source = document!.metadata.hasPendingWrites ? "Local" : "Server"
print("Updating item data from the \(source).")
Item.listenerSet = true
Item.list = document?.get("List") as! [String:[String:Any]]
completion("Item data is set")
})
} else {
completion("Item listener already exists")
}
}
func add(itemID:String) {
let itemRef = db.collection("Items").document("default")
itemRef.setData([
"List": [
itemID : [
"Count": FieldValue.increment(1.0),
]]
], merge:true)
}
FirstViewController:UIViewController
let item = Item()
var itemList = [[String:Any]]()
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var switchBar: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Setting the table views
tableView.delegate = self
tableView.dataSource = self
}
override func viewDidAppear(_ animated: Bool) {
print("View Did appear") // prints OK
item.setListener() { (result) in
print(result)
loadData()
}
}
func loadData() {
itemList.removeAll()
var counter = 0
for (id,data) in Item.list {
itemList.append(data)
itemList[counter]["ID"] = id
counter += 1
}
tableView.reloadData()
}
@IBAction func switchBarChanged(_ sender: UISegmentedControl) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
switch switchBar.selectedSegmentIndex {
case 1:
let vc = storyBoard.instantiateViewController(identifier: "secondViewController")
show(vc, sender: self)
default:
break
}
}
扩展FirstViewController:UITableViewDataSource,UITableViewDelegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("These are items:\(itemList)")
print(itemList.count)
print(tableView.bounds.height)
return itemList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell") as! ItemCell
let itemID = itemList[indexPath.row]["ID"] as! String
let itemName = itemList[indexPath.row]["Name"] as! String
let itemCount = itemList[indexPath.row]["Count"] as! Double
cell.itemID = itemID
cell.itemName.text = itemName
cell.itemCount.text = String(itemCount)
print("Items within the tableView \(itemList)") // prints only in the first run whenever there is an upload, or when refreshed.
return cell
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let add = UIContextualAction(style: .normal, title: "Add", handler: { (action, view, completionHandler) in
item.add(itemID:(itemList[indexPath.row]["ID"] as! String))
completionHandler(true)
})
add.image = UIImage(systemName: "plus")
let configuration = UISwipeActionsConfiguration(actions: [add])
configuration.performsFirstActionWithFullSwipe = true
return configuration
}
类 ItemCell:UITableViewCell
var itemID = String()
@IBOutlet weak var itemName: UILabel!
@IBOutlet weak var itemCount: UILabel!
SecondViewController:UIViewController
@IBOutlet weak var switchBar: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func switchBarChanged(_ sender: UISegmentedControl) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
switch switchBar.selectedSegmentIndex {
case 0:
let vc = storyBoard.instantiateViewController(identifier: "firstViewController")
show(vc, sender: self)
default:
break
}
}
现在这是我遇到问题的流程;
- 应用程序加载,
FirstViewController
加载,viewDidAppear
启动。tableView
加载。 - 当向右滑动时,firestore 服务器中的值增加一,快照监听器启动,tableview 重新加载,一切都按预期工作。
- 在我切换到
SecondViewController
并返回到 后FirstViewController
,viewDidAppear
开始tableView
加载。 - 然而,这一次,当我再次向右滑动时,firestore 服务器中的值仍会增加 1,并且快照侦听器会启动,更新
Item.list
.static var ItemList
得到更新,并且 tableView 确实重新加载,但是这一次,只有numberOfRowsInSection
方法工作,因为它打印计数和更新的 itemList,然后什么都没有发生cellForRowAt
不起作用并且行没有得到更新。 - 现在在这个阶段,如果我
SecondViewController
返回并FirstViewController
更新行,但向右滑动仍然无济于事。
我在这里想念什么?谢谢大家的回复。
解决方案
推荐阅读
- android - Android Json Post 显示 JSONExepection(字符 0 处的输入结束)
- 4d-database - 如何避免 EntitySelection.toCollection($fieldNames) 中不正确的字段名称引发错误
- c++ - 如何在 C++ 中调用基类的参数化构造函数?
- javascript - 使用javascript更改图像幻灯片中的点样式
- glade - 新版本的 GLADE /“将小部件添加为顶层”
- mysql - centos 6.9无法启动mysql服务器
- apache-spark - Nagios 服务器如何与远程节点通信,即它在代理和无代理设置中使用哪种协议?
- haskell - 是什么导致了这种类型的歧义?
- r - 如何估计 R 脚本运行时间?
- python - 如何在 sqlite3 的所需列中插入数据?