简体   繁体   English

Swift Eureka SplitRow 值更新

[英]Swift Eureka SplitRow values updating

I'm trying to update values of each LabelRow in my SplitRow every time VC appears one the screen (it has to be like that).每次 VC 出现在屏幕上时,我都会尝试更新 SplitRow 中每个 LabelRow 的值(必须是这样)。 I've tried using.cellUpdate on both LabelRows but it just crashes.我在两个 LabelRows 上都尝试过 using.cellUpdate ,但它只是崩溃了。 When I use.updateCell on just one of LabelRows, it updates value of that row just fine.当我只在其中一个 LabelRows 上使用 .updateCell 时,它会很好地更新该行的值。 Is there a way to update both of them at the same time?有没有办法同时更新它们? I tried to use.updateCell on SplitRow but then I can't update values (they are read-only?).我尝试在 SplitRow 上使用 .updateCell 但我无法更新值(它们是只读的?)。 The failing code part:失败的代码部分:

            <<< 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: here's what I've tried编辑:这是我尝试过的

            <<< 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
            }
        } 

And other one还有一个

            <<< 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
        }

Your code crashes because of a stack overflow.您的代码由于堆栈溢出而崩溃。 The setter of $1.value calls cell.update() , which calls cellUpdate , which forms an infinite loop:( $1.value的设置器调用cell.update() ,它调用cellUpdate ,其中 forms 是一个无限循环:(

I've found this dirty trick that kind of works:我发现这种肮脏的把戏很管用:

Wrap the row.value =... line in a DispatchQueue.main.async call: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 = ...
        }
    }
}

In reality though, you should find another place to set your row's value.但实际上,您应该找到另一个位置来设置行的值。 It sounds like the value changes over time and you want to always show the latest value.听起来值会随着时间而变化,并且您希望始终显示最新值。 Try using a Reactive approach using RxSwift .尝试使用RxSwift使用 Reactive 方法。 You'd subscribe to an Observable<String> , and set each value you receive to the row's value.您将订阅Observable<String> ,并将收到的每个值设置为该行的值。

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

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