自定义表视图SwiftUI iOS

自定义表视图SwiftUI iOS,swiftui,Swiftui,我需要创建一个表格,以便单元格中的文本扩展到所需的大小。 我创建内容视图: struct ContentView: View { let items: [String] = [ "1 One Line", "1 One Line", "1 One Line" ] var body: some View { ScrollView {

我需要创建一个表格,以便单元格中的文本扩展到所需的大小。 我创建内容视图:

struct ContentView: View {
    let items: [String] = [
        "1 One Line",
        "1 One Line",
        "1 One Line"
    ]
    var body: some View {
        ScrollView {
            VStack(spacing: 1) {
                ForEach(self.items, id: \.hash) { (item) in
                    HStack(spacing: 1) {
                        Text(item)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(width: 70)
                            .background(Color.white)
                        Text("One Line")
                            .frame(width: 70)
                            .background(Color.white)
                    }
                }
                // MARK: Total
                HStack(spacing: 1) {
                    Text("Title")
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(Color.white)
                    Text("One Line")
                        .frame(width: 141)
                        .background(Color.white)
                }
            }
            .padding(1)
            .background(Color.orange)
        }
        .padding(15)
    }
}
而且效果很好: 但如果文本变为多行,则表格将中断:

let items: [String] = [
    "1 One Line",
    "2 Two Line\nTwo Line",
    "3 Three lines\nThree lines\nThree lines"
]
边界越来越宽:


如果使maxHeight=infinity,则可以解决边界问题,但线条将等于最大高度。

可能的解决方案是将
maxHeight:.infinity
一起使用。fixedSize

var body: some View {
        ScrollView {
            VStack(spacing: 1) {
                ForEach(self.items, id: \.hash) { (item) in
                    HStack(spacing: 1) {
                        Text(item)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                            
                    }
                    .fixedSize(horizontal: false, vertical: true) //<---Here
                }
                // MARK: Total
                HStack(spacing: 1) {
                    Text("Title")
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(Color.white)
                    Text("One Line")
                        .frame(width: 137)
                        .background(Color.white)
                }
            }
            .padding(1)
            .background(Color.orange)
        }
        .padding(15)
    }
var主体:一些视图{
滚动视图{
VStack(间距:1){
中的ForEach(self.items,id:\.hash){(item)
HStack(间距:1){
文本(项目)
.frame(最大宽度:。无穷大,对齐:。中心)
.背景(颜色.白色)
文本(“一行”)
.frame(最大高度:。无穷大,对齐:。中心)
.背景(颜色.白色)
文本(“一行”)
.frame(最大高度:。无穷大,对齐:。中心)
.背景(颜色.白色)
}

.fixedSize(水平:false,垂直:true)/可能的解决方案是使用
maxHeight:.infinity
.fixedSize

var body: some View {
        ScrollView {
            VStack(spacing: 1) {
                ForEach(self.items, id: \.hash) { (item) in
                    HStack(spacing: 1) {
                        Text(item)
                            .frame(maxWidth: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                        Text("One Line")
                            .frame(maxHeight: .infinity, alignment: .center)
                            .background(Color.white)
                            
                    }
                    .fixedSize(horizontal: false, vertical: true) //<---Here
                }
                // MARK: Total
                HStack(spacing: 1) {
                    Text("Title")
                        .frame(maxWidth: .infinity, alignment: .center)
                        .background(Color.white)
                    Text("One Line")
                        .frame(width: 137)
                        .background(Color.white)
                }
            }
            .padding(1)
            .background(Color.orange)
        }
        .padding(15)
    }
var主体:一些视图{
滚动视图{
VStack(间距:1){
中的ForEach(self.items,id:\.hash){(item)
HStack(间距:1){
文本(项目)
.frame(最大宽度:。无穷大,对齐:。中心)
.背景(颜色.白色)
文本(“一行”)
.frame(最大高度:。无穷大,对齐:。中心)
.背景(颜色.白色)
文本(“一行”)
.frame(最大高度:。无穷大,对齐:。中心)
.背景(颜色.白色)
}
.fixedSize(水平:假,垂直:真)//