Deletable Table with TextField on SwiftUI

前端 未结 2 723
故里飘歌
故里飘歌 2020-11-28 15:19

Environment

  • Xcode 11.2.1 (11B500)

Problem

In order to implement editable teble with TextField on SwiftUI, I used ForEach(0..&l

相关标签:
2条回答
  • 2020-11-28 15:58

    it is because editbutton is IN your list. place it ouside or better in navigationbar.

    0 讨论(0)
  • 2020-11-28 16:14

    Ok, the reason is in documentation for used ForEach constructor (as you see range is constant, so ForEach grabs initial range and holds it):

    /// Creates an instance that computes views on demand over a *constant*
    /// range.
    ///
    /// This instance only reads the initial value of `data` and so it does not
    /// need to identify views across updates.
    ///
    /// To compute views on demand over a dynamic range use
    /// `ForEach(_:id:content:)`.
    public init(_ data: Range<Int>, @ViewBuilder content: @escaping (Int) -> Content)
    

    So the solution would be to provide dynamic container. Below you can find a demo of possible approach.

    Full module code

    import SwiftUI
    
    struct DummyView: View {
        @State var animals: [String] = ["                                                                    
    0 讨论(0)
提交回复
热议问题