[英]Dynamically loading options for a row
This is a question related to the Swift open source project called Eureka and I'm posting this question here on SO since they monitor their tag here.这是一个与名为Eureka的 Swift 开源项目相关的问题,我在这里发布了这个问题,因为他们在这里监控他们的标签。
Okay so I create a form in the viewDidLoad
method of a view controller.好的,所以我在视图控制器的viewDidLoad
方法中创建了一个表单。 At the same time I make a http call to an API to get a list of data (jobs).同时,我对 API 进行 http 调用以获取数据(作业)列表。 This call is asynchronous.此调用是异步的。 I want to display these jobs as option values in a PushRow
.我想在PushRow
这些作业显示为选项值。
Since this API call is asynchronous, by the time this data is received, the form is already set up.由于此 API 调用是异步的,因此在收到此数据时,表单已设置好。 I have a class level array setup to be populated with the list of jobs from the http call.我有一个类级数组设置,用于填充来自 http 调用的作业列表。 That same array is assigned to that row's options
property.相同的数组被分配给该行的options
属性。 I assign the values to the array when I receive the data.当我收到数据时,我将值分配给数组。 But then when I go and tap on the PushRow
, it's empty.但是当我去点击PushRow
,它是空的。
class CreateEventViewController: FormViewController {
private var jobs = [Job]()
override func viewDidLoad() {
super.viewDidLoad()
setupForm()
api.getJobs(.LeadCreated) { jobs, error in
if let error = error {
print(error.localizedDescription)
return
}
if let jobs = jobs {
self.jobs = jobs
}
}
}
private func setupForm() {
form
+++ Section()
<<< PushRow<String>("Jobs") {
$0.title = "Choose Job"
$0.options = jobs.map { $0.description }
$0.selectorTitle = "Choose..."
}.onPresent { from, to in
to.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Done, target: from, action: #selector(CreateEventViewController.multipleSelectorDone(_:)))
}
}
}
Is there some sort of a way to refresh or reload the data for a specific row or the form as a whole?是否有某种方法可以刷新或重新加载特定行或整个表单的数据?
Update 1更新 1
As per Kevin's answer below, I modified my code as below.根据下面凯文的回答,我修改了我的代码如下。
override func viewDidLoad() {
super.viewDidLoad()
api.getJobs(.LeadCreated) { jobs, error in
if let error = error {
print(error.localizedDescription)
return
}
if let jobs = jobs {
self.jobs = jobs
if let jobsRow = self.form.rowByTag("Jobs") {
jobsRow.updateCell()
}
}
}
setupForm()
}
But for some reason, it's still not working.但由于某种原因,它仍然无法正常工作。 The options are empty.选项为空。 The cell is correctly picked up via the tag, I checked.我检查了通过标签正确拾取单元格。 Anyway failing this, I also tried tableView.reloadData()
as well but to no avail.无论如何失败了,我也尝试过tableView.reloadData()
但无济于事。
I found it!我找到了! You had to add the .cellUpdate {...}
closure to the row and inside it assign the values to the options
property.您必须将.cellUpdate {...}
闭包添加到行中,并在其中将值分配给options
属性。 Like so,像这样,
<<< PushRow<String>("Jobs") {
$0.title = "Choose Job"
$0.options = jobs.map { $0.description }
$0.selectorTitle = "Choose..."
}
.cellUpdate { cell, row in
row.options = self.jobs.map { $0.description }
}
At the bottom of the README in the FAQ section there is a reference to reloading在常见问题部分的 README 底部有一个关于重新加载的参考
How to set the form values using a dictionary如何使用字典设置表单值
... If the form was already displayed we have to reload the visible rows either by reloading the table view
tableView.reloadData()
or invokingupdateCell()
to each visible row. ...如果表单已经显示,我们必须通过重新加载表视图tableView.reloadData()
或调用updateCell()
到每个可见行来重新加载可见行。
However, I don't even think that's necessary.但是,我什至认为这没有必要。 Just call setupForm()
after you update self.jobs
.只需在更新self.jobs
后调用setupForm()
self.jobs
。
You can see from the FormViewController
source code that when form
is set, the tableview is reloaded.从FormViewController
源代码中可以看到,当form
被设置时,tableview 会被重新加载。
public var form : Form {
get { return _form }
set {
_form.delegate = nil
tableView?.endEditing(false)
_form = newValue
_form.delegate = self
if isViewLoaded() && tableView?.window != nil {
tableView?.reloadData()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.