简体   繁体   English

快速呈现具有动态内容的静态单元格

[英]Swift rendering static cells with dynamic content

I have a static UITableView with n columns and 1 section. 我有一个带有n列和1部分的静态 UITableView The cells are detailed with some dynamic content passed from another UIViewController . 单元格中详细介绍了从另一个UIViewController传递的一些动态内容。
My solution is the following: I make a call in viewDidAppear to present the dynamic content and eventually reload the view: 我的解决方案如下:我在viewDidAppear进行调用以呈现动态内容并最终重新加载视图:

override func viewDidAppear(_ animated: Bool) {
        setupDetails()
    }

fileprivate func setupDetails() {
        tableView.cellForRow(at: [0,0] as IndexPath)?.detailTextLabel?.text = "..."
        tableView.cellForRow(at: [0,1] as IndexPath)?.detailTextLabel?.text = "..."
        // .... multiple cells

        tableView.reloadData()
    }

The thing I don't like is that the content is updated after the view is shown to the user (thanks to the viewDidAppear call). 我不喜欢的是在向用户显示视图后更新内容(这要感谢viewDidAppear调用)。
For what concerns working on the cellForRow() method, I could not have it working given the fact that the cell is statically created in my Storyboard. 对于在cellForRow()方法上需要处理的问题,鉴于该单元是在我的情节提要中静态创建的,因此我无法使其正常工作。
My question is if there exists a pattern to be used in this specific case which I didn't think of. 我的问题是在这种特殊情况下是否存在一种我没有想到的模式。 I basically want the view to be loaded before the view is presented, as if I was working with dynamic cells. 我基本上希望在呈现视图之前先加载视图,就像我在处理动态单元格一样。

My question is if there exists a pattern to be used in this specific case 我的问题是在这种特定情况下是否存在要使用的模式

Yes. 是。 Don't use a static table. 不要使用静态表。 Your table is not static! 您的桌子不是静态的! "Static" means "never changes". “静态”是指“永不变”。 Your table is the opposite of static; 您的表与static相反; it does change. 确实改变了。 It is dynamic! 它是动态的!

So, use an ordinary dynamic table. 因此,请使用普通的动态表。 Maintain a data model that is consulted by the Three Big Questions (the data source methods). 维护三个主要问题(数据源方法)所参考的数据模型。 To update the table with new data, first update the data model, and then call reloadData . 要使用新数据更新表,请首先更新数据模型,然后调用reloadData That will cause cellForRowAt to be called for you, and you'll populate each cell from the data model. 这将导致为您调用 cellForRowAt ,然后您将填充数据模型中的每个单元格。 That is much more efficient than what you're doing. 这比您正在执行的效率高得多。

As for the flash, that's just a matter of timing; 至于闪光灯,那只是时间问题。 use viewWillAppear instead of viewDidAppear . 使用viewWillAppear而不是viewDidAppear

Don't use ViewDidAppear if you don't want you change to show to the user. 如果您不希望更改以显示给用户,则不要使用ViewDidAppear。 Use ViewWillAppear and the view will already be in its final state when the user first sees it. 使用ViewWillAppear,当用户第一次看到该视图时,该视图将已经处于其最终状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM