我试图在每次VC出现在屏幕上时更新我的SplitRow中的每个LabelRow的值(必须是这样的)。我试过在两个LabelRows上使用.cellUpdate,但它只是崩溃了。当我在其中一个LabelRows上使用.updateCell时,它能很好地更新该行的值。有什么方法可以同时更新它们的值吗?我试着在SplitRow上使用.updateCell,但是我无法更新值(它们是只读的?).失败的代码部分。
<<< SplitRow<LabelRow, LabelRow>() {
$0.rowLeftPercentage = 0.5
$0.rowLeft = LabelRow() {
$0.title = "Expected"
$0.tag = "temp_expected"
} //tried callbacks here
$0.rowRight = LabelRow() {
$0.title = "Last"
$0.tag = "temp_last"
} //tried callbacks
} //also tried there but cant update values
EDIT:这是我试过的方法。
<<< SplitRow<LabelRow, LabelRow>() {
$0.rowLeftPercentage = 0.5
$0.rowLeft = LabelRow() {
$0.title = "Expected"
$0.tag = "temp_expected"
}.cellUpdate {
$1.value = "value1" //here would go value from other object, doesn't work either
}
$0.rowRight = LabelRow() {
$0.title = "Last"
$0.tag = "temp_last"
} .cellUpdate {
$1.value = "value2" // same as above
}
}
还有一个
<<< SplitRow<LabelRow, LabelRow>() {
$0.rowLeftPercentage = 0.5
$0.rowLeft = LabelRow() {
$0.title = "Expected"
$0.tag = "temp_expected"
}
$0.rowRight = LabelRow() {
$0.title = "Last"
$0.tag = "temp_last"
}
}.cellUpdate {
$1.value?.left = "value1" //does nothing
$1.value?.right = "value2" //does nothing
}
你的代码由于堆栈溢出而崩溃。的设置者 $1.value
电话 cell.update()
,这就要求 cellUpdate
这就形成了一个无限循环 :(
我发现这个 阴招 那种工作。
包住 row.value = ...
一行 DispatchQueue.main.async
调用。
SplitRow<LabelRow, LabelRow>() {
$0.rowLeftPercentage = 0.5
$0.rowLeft = LabelRow() {
$0.title = "A"
$0.tag = "temp_expected"
}.cellUpdate { cell, row in
DispatchQueue.main.async {
row.value = ...
}
}
$0.rowRight = LabelRow() {
$0.title = "B"
$0.tag = "temp_last"
} .cellUpdate { cell, row in
DispatchQueue.main.async {
row.value = ...
}
}
}
但实际上,你应该找到另一个地方来设置你的行的值。听起来,值是随着时间的推移而变化的,你希望总是显示最新的值。试着使用Reactive方法,使用 RxSwift. 你会订阅一个 Observable<String>
,并将你收到的每个值设置为该行的值。