问题描述
我有两个相同的项目。一个是用 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
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)