【问题标题】:How to get row index in SwiftUI List?如何在 SwiftUI 列表中获取行索引?
【发布时间】:2019-09-08 13:07:41
【问题描述】:

我想要一个编号列表,其中每一行都有一个数字。

类似的东西

但我在 List 初始化程序中没有看到正确的 API

此时我看到了这个解决方法

var persons = ["Boris", "Anna", "Tom"]

// MARK: - Body
func body(props: Props) -> some View {
    List(persons.indices, id: \.self) { index in
        Text("\(index) \(self.persons[index])")
    }
}

【问题讨论】:

    标签: swift swiftui swiftui-list


    【解决方案1】:

    使用 .indices() 不是一种解决方法,它是一种正确的方法。

    或者,您也可以将发行说明中的​​代码用于 indexed() 数组:

    struct ContentView: View {
        var persons = ["Boris", "Anna", "Tom"]
    
        var body: some View {
            VStack {
                List(persons.indexed(), id: \.1.self) { idx, person in
                    Text("\(idx) - \(person)")
                }
            }
        }
    }
    
    
    // This is taken from the Release Notes, with a typo correction, marked below
    struct IndexedCollection<Base: RandomAccessCollection>: RandomAccessCollection {
        typealias Index = Base.Index
        typealias Element = (index: Index, element: Base.Element)
    
        let base: Base
    
        var startIndex: Index { base.startIndex }
    
       // corrected typo: base.endIndex, instead of base.startIndex
        var endIndex: Index { base.endIndex }
    
        func index(after i: Index) -> Index {
            base.index(after: i)
        }
    
        func index(before i: Index) -> Index {
            base.index(before: i)
        }
    
        func index(_ i: Index, offsetBy distance: Int) -> Index {
            base.index(i, offsetBy: distance)
        }
    
        subscript(position: Index) -> Element {
            (index: position, element: base[position])
        }
    }
    
    extension RandomAccessCollection {
        func indexed() -> IndexedCollection<Self> {
            IndexedCollection(base: self)
        }
    }
    

    【讨论】:

    • 我试过了,但它对我不起作用,构建失败并且没有弹出错误......
    • @kontiki,是的,它有效,但是当我在 List/ForEach 中添加 if 条件时出现错误“类型 '_' 没有成员 '1'”。
    【解决方案2】:

    您可以只使用enumerated,例如https://github.com/onmyway133/blog/issues/515

    struct CountriesView: View {
        let countries: [Country]
    
        var body: some View {
            let withIndex = countries.enumerated().map({ $0 })
    
            return List(withIndex, id: \.element.name) { index, country in
                NavigationLink(
                    destination: CountryView(country: country),
                    label: {
                        VStack(alignment: .leading) {
                            Text(country.name)
                                .styleMultiline()
                        }
                        .paddingVertically()
                    }
                )
            }
        }
    }
    

    【讨论】:

      【解决方案3】:

      这是苹果的例子:

      var landmarkIndex: Int {
      userData.landmarks.firstIndex(where: { $0.id == landmark.id })!}
      

      为你:

      var persons = ["Boris", "Anna", "Tom"]
      
      // MARK: - Body
      
      func body(props: Props) -> some View {
      List(persons.indices, id: \.self) { index in
      
      var i: Int {
      persons.firstIndex(where: { $0.id == index.id })!}
      
          Text("\(i) \(self.persons[i])")
      }}
      

      【讨论】:

        猜你喜欢
        • 2022-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        相关资源
        最近更新 更多