在 SwiftUI 中创建 1000 个元素的列表时的性能问题

jsl*_*oop 6 ios swift swiftui

我有一个带有列表的水平滚动视图。水平滚动时,如何使列表对齐边缘。

struct RowView: View {
    var post: Post
    var body: some View {
        GeometryReader { geometry in
            VStack {
                Text(self.post.title)
                Text(self.post.description)
            }.frame(width: geometry.size.width, height: 200)
            //.border(Color(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)))
            .background(Color(#colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1)))
            .cornerRadius(10, antialiased: true)
            .padding(EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0))
        }
    }
}

struct ListView: View {
    var n: Int
    @State var posts = [Post(id: UUID(), title: "1", description: "11"),
                        Post(id: UUID(), title: "2", description: "22"),
                        Post(id: UUID(), title: "3", description: "33")]

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                ForEach(0..<self.n) { n in
                    RowView(post: self.posts[0])
                    //.border(Color(#colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)))
                    .frame(width: geometry.size.width, height: 200)
                }
            }
        }
    }
}

struct ContentView: View {
    init() {
        initGlobalStyles()
    }

    func initGlobalStyles() {
        UITableView.appearance().separatorColor = .clear
    }

    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ScrollView(.horizontal) {
                    HStack {
                        ForEach(0..<3) { _ in
                            ListView(n: 1000)  // crashes
                                .frame(width: geometry.size.width - 60)
                        }
                    }.padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 0))
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我给出 的值时ListView(n: 1000),视图崩溃了。该应用程序启动并显示白屏一段时间,然后出现黑屏。

2019-10-06 15:52:57.644766+0530 MyApp[12366:732544] [Render] CoreAnimation: Message::send_message() returned 0x1000000e
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?我的假设是它会使用类似 dequeue cell 这样的东西UITableView,但不确定它为什么会崩溃。

LuL*_*aGa 8

提供的代码有几个问题。最重要的是你是不是使用List只是一个ForEach嵌套的ScrollView,这就好比相当于把1000个UIViews在UIStack -不是非常有效。还有很多硬编码的维度,其中不少是重复的,但是在计算视图时增加了很大的负担。

我已经简化了很多,它以 n = 10000 运行而不会崩溃:

struct ContentView: View {

    var body: some View {
        GeometryReader { geometry in
            NavigationView {
                ScrollView(.horizontal) {
                    HStack {
                        ForEach(0..<3) { _ in
                            ListView(n: 10000)
                                .frame(width: geometry.size.width - 60)
                        }
                    }   .padding([.leading], 10)
                }
            }
        }
    }
}

struct ListView: View {

    var n: Int

    @State var posts = [Post(id: UUID(), title: "1", description: "11"),
                        Post(id: UUID(), title: "2", description: "22"),
                        Post(id: UUID(), title: "3", description: "33")]

    var body: some View {
        List(0..<self.n) { n in
            RowView(post: self.posts[0])
                .frame(height: 200)
        }
    }
}

struct RowView: View {

    var post: Post

    var body: some View {
        HStack {
            Spacer()
            VStack {
                Spacer()
                Text(self.post.title)
                Text(self.post.description)
                Spacer()
            }
            Spacer()
        }   .background(RoundedRectangle(cornerRadius: 10)
                            .fill(Color(#colorLiteral(red: 0.721568644, green: 0.8862745166, blue: 0.5921568871, alpha: 1))))
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我想强调的是,在视图本身上使用“RoundedRectangle”而不是“cornerRadius”修饰符对性能有多大影响。我的列表刚刚从滞后变为完全平滑。 (4认同)

Moj*_*ini 5

ScrollView不要重复使用任何东西。但是List做。

所以改变这个:

ScrollView {
    ForEach(0..<self.n) { n in
        ,,,
    }
}
Run Code Online (Sandbox Code Playgroud)

对此:

List(0..<self.n) { n in
    ,,,
}
Run Code Online (Sandbox Code Playgroud)

SwiftUI 2.0

您可以使用Lazy堆栈,例如LazyVStackLazyHStack。因此,即使您将它们与 一起使用ScrollView,它也会流畅且高效。