SwiftUI元素列表使用列表-滚动

时间:2020-04-13 14:28:50

标签: swift layout swiftui

我正在尝试显示一个大数据表(超过100行乘100列)。我将数据保存在二维数组中:

import SwiftUI

struct PivotCell : Hashable {
    enum CellType {
        case columnHeader, rowHeader, value, total, grandTotal, blank
    }

    let id = UUID()
    let cellType : CellType
    let contents : String

    static func == (lhs: PivotCell, rhs: PivotCell) -> Bool {
        return lhs.id == rhs.id
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }

    init(cellType: PivotCell.CellType, contents: String ) {
        self.cellType = cellType
        self.contents = contents
    }
}

struct PivotTableView: View {
    var displayData : [[PivotCell]]

    var body: some View {

        GeometryReader { geometry in
            List(self.displayData, id: \.self) { row in
                HStack {
                    ForEach(row, id: \.id) { cell  in
                        PivotCellView(cell)
                    }
                }
            }.frame(width: geometry.size.width, height: geometry.size.height).id(UUID())
        }
    }
}

struct PivotCellView: View {
    let cell: PivotCell

    var body: some View {
        Text(cell.contents)
            .frame(width: 100, height: 30, alignment: .center)
    }

    init(_ cell: PivotCell) {
        self.cell = cell
    }
}



struct PivotTableView_Previews: PreviewProvider {

    static  var cell = PivotCell(cellType: .value, contents: "123")
    static var displayData = [[cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell],
        [cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell, cell]]

    static var previews: some View {
        PivotTableView(displayData: displayData)
    }
}

我试图:

  1. 将其包装在ScrollView(.horizo​​ntal)中-它消失了
  2. 在ScrollView(.horizo​​ntal)中使用VStack代替List。 可以,但是性能很差,因为我们不能从List的单元中受益 重用

有什么办法解决这个问题吗?

0 个答案:

没有答案