I created an excel-like view, using a multi-directional scroll view. Now I want to pin the headers, not only the column headers but the row headers as well. Look at the following gif:
Code I used to create this view:
ScrollView([.vertical, .horizontal]){
VStack(spacing: 0){
ForEach(0..<model.rows.count+1, id: \.self) {rowIndex in
HStack(spacing: 0) {
ForEach(0..<model.columns.count+1) { columnIndex in
if rowIndex == 0 && columnIndex == 0 {
Rectangle()
.fill(Color(UIColor(Color.white).withAlphaComponent(0.0)))
.frame(width: CGFloat(200).pixelsToPoints(), height: CGFloat(100).pixelsToPoints())
.padding([.leading, .trailing])
.border(width: 1, edges: [.bottom, .trailing], color: .blue)
} else if (rowIndex == 0 && columnIndex > 0) {
TitleText(
label: model.columns[columnIndex - 1].label,
columnWidth: CGFloat(columnWidth).pixelsToPoints(),
borderEgdes: [.top, .trailing, .bottom]
)
} else if (rowIndex > 0 && columnIndex == 0) {
TitleText(
label: model.rows[rowIndex - 1].label,
columnWidth: CGFloat(columnWidth).pixelsToPoints(),
borderEgdes: [.trailing, .bottom, .leading]
)
} else if (rowIndex > 0){
//text boxes
let column = model.columns[columnIndex - 1]
switch column.type {
case "Text":
MatrixTextField(keyboardType: .default)
case "Number":
MatrixTextField(keyboardType: .decimalPad)
case "RadioButton":
RadioButton()
case "Checkbox":
MatrixCheckbox()
default:
MatrixTextField(keyboardType: .default)
}
}
}
}
}
}
}
.frame(maxHeight: 500)
Is it possible to pin both column and row headers here?
It's important I use VStack and HStack only instead of LazyVStack and LazyHStack, as I need smoothness while scrolling, when I use Lazy stacks, it jitters a lot for obvious reasons. So cannot really use section headers here.
What other approach could I follow?