微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI LazyVStack 与 UITableView CPU 使用率

如何解决SwiftUI LazyVStack 与 UITableView CPU 使用率

我有两个相同的项目。一个是用 SwiftUI 编写的,另一个是用 UIKit 编写的。但是当 UIKit 消耗 5% 的 cpu 时,SwiftUI 消耗 26% 的 cpu。有谁知道如何减少 SwiftUI 使用的 cpu

我最初的问题是 List vs UITableView。但是我看到 Lazyvstack 比 List 高效得多,这要归功于 @Paulw11。 但它仍然远远落后于 UITableView。

import SwiftUI

struct ContentView: View {
    
    @State private var progress: Double = 0.0
        
    var body: some View {
        vstack {
            ScrollView {
                Lazyvstack {
                    ForEach(1...1000,id: \.self) { value in
                        Text("Hello,world!")
                    }
                }
            }
            Slider(value: $progress,in: 0.0...1.0,step: 0.01)
        }
        .onChange(of: progress,perform: { value in
            print(value)
        })
    }
}
import UIKit

final class ViewController: UIViewController {
    
    private let array: Array<String> = Array(repeating: "Hello,world!",count: 1000)
    
    @IBOutlet weak var tableView: UITableView!
    
    @IBOutlet weak var slider: UiSlider!
    
    @IBAction func sliderChanged(_ sender: UiSlider) {
        print(sender.value)
    }
}

extension ViewController: UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        array.count[enter image description here][1]
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell",for: indexPath)
        cell.textLabel?.text = array[indexPath.row]
        return cell
    }
}

UITableView Project Photo

SwiftUI Project Photo

Download sample projects

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。