ScrollView:如何处理大量内容?

时间:2019-08-04 10:18:21

标签: swiftui

这段代码是对import Foundation import UIKit class VC_catalogo_modelos: UIViewController { @IBOutlet weak var collectionViewModelos: UICollectionView! var arrayModelos = ["1","2","3","4","5","6","7","8","9","10","11","12"] var espacioCollectionAncho = 0.0 var espacioCollectionAlto = 0.0 override func viewDidLoad() { super.viewDidLoad() //asigno el gesto que muestra el menu lateral if revealViewController() != nil { self.view.addGestureRecognizer(revealViewController().panGestureRecognizer()) self.collectionViewModelos.addGestureRecognizer(revealViewController().panGestureRecognizer()) } NotificationCenter.default.addObserver(self, selector: #selector(deviceRotated), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) recalcularSizeCeldas() collectionViewModelos.reloadData() } @IBAction func botonVolverFamilias(_ sender: Any) { //instancio el viewcontroller de familias let sw = revealViewController() self.view.window?.rootViewController = sw let viewControllerModelos = storyboard!.instantiateViewController(withIdentifier: "vc_catalogo_familias") as! VC_catalogo //asigno la vista del menu lateral y presento el viewcontroller let navigationController = UINavigationController(rootViewController: viewControllerModelos) navigationController.navigationBar.isHidden=false navigationController.setNavigationBarHidden(true, animated: false) sw!.setFront(navigationController, animated: true) } //calculo tamaño de la celda de modelo func recalcularSizeCeldas(){ //HORIZONTAL if UIDevice.current.orientation.isLandscape { let width = (view.frame.size.width - 46) / 4 let height = (view.frame.size.height - 156) / 3 let layout = collectionViewModelos.collectionViewLayout as! UICollectionViewFlowLayout layout.itemSize = CGSize(width: width, height: height) } //VERTICAL else { let width = (view.frame.size.width - 36) / 3 let height = (view.frame.size.height - 166) / 4 let layout = collectionViewModelos.collectionViewLayout as! UICollectionViewFlowLayout layout.itemSize = CGSize(width: width, height: height) } } @objc func deviceRotated(){ recalcularSizeCeldas() } } extension VC_catalogo_modelos: UICollectionViewDataSource{ func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.arrayModelos.count } //funcion que recorre todas las celdas antes de presentarlas func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //instancio el modelo de celta let cell = collectionViewModelos.dequeueReusableCell(withReuseIdentifier: "cellModelos", for: indexPath) as! VC_catalogo_modelos_cell return cell } //cuando se pincha un producto del tableview internal func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let cell = collectionViewModelos.dequeueReusableCell(withReuseIdentifier: "cellModelos", for: indexPath) as! VC_catalogo_modelos_cell } } extension VC_catalogo_modelos: UIViewControllerTransitioningDelegate, UINavigationControllerDelegate { func navigationController( _ navigationController: UINavigationController, animationControllerFor operation: UINavigationController.Operation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { //This tells our navigation controller the style that we want to animate our transition between view controllers let simpleOver = SimpleOver(direction: .right) simpleOver.popStyle = (operation == .pop) return simpleOver } } 的压力测试。虽然ScrollView循环中包含3000个项目的渲染速度可以接受,但是30000的时间很长,而300000的时间是永远的(我在3分钟后停止了)。

但是,在某些情况下,您需要滚动很多内容,请想象一个具有可缩放比例(十年/年/月/日/天)的时间轴。在这种情况下,可能需要跨越50年才能显示几天,并且用户必须有机会放大和缩小才能更改比例。

因此,对我来说,问题是:使用SwiftUI可以采取哪些策略来优化缓存,预取等,或者找出用户滚动后接下来要显示的部分,并防止模型计算宽度?

更准确地说:模型知道所有要显示的数据。 ForEach必须提供一个内容视图,该视图具有要显示的所有项目的宽度。这个宽度是我的问题:在没有所有可用商品的情况下,如何以一种合理的方式确定宽度?

ScrollView

2 个答案:

答案 0 :(得分:1)

过去有一种技巧,就是使用水平if (i % 4 == 0) { phraseDivIndex++; const newContainer = document.createElement('div'); newContainer.classList.add('phraseDiv', 'catRow'); newContainer.tabIndex = 0; document.getElementById("entries").appendChild(newContainer) } 来进行反向填充(从下到上),您可以在这里使用它:

  • 使用垂直列表
  • 旋转90Ro
  • 旋转它的所有项目-90˚

现在您有水平Import pandas as pd df = pd.read_excel("yourfile.xlsx")

Import pandas as pd
df1 = pd.read_excel("file1.xlsx")
df2 = pd.read_excel("file2.xlsx")
df3 = df2.merge(df1, on='NOME')
df3.to_excel("mergeFile.xlsx")

答案 1 :(得分:0)

为什么要使用Scrollview而不是ListList将重用单元,这对于性能和内存管理来说要好得多。有了列表,当单元格滚动到视线之外时,它就会从视图中删除,这对性能有很大帮助。

有关性能差异的更多信息,请参见here

    var body: some View
    {
        List
        {
            ForEach(1..< 30000) {
                index in

                Rectangle()
                    .fill(Color.yellow)
                    .frame(minWidth: 100, idealWidth: 200, maxWidth: 300, minHeight: 500, idealHeight: 500, maxHeight: 500, alignment: .bottom)
            }
        }
    }

编辑:由于我没有阅读原始问题,因为滚动视图是水平的。

一种选择是将UICollectionView包装到UIViewRepresentable中,请参见here for the dev talkhere for an example implementation,这将使您将其显示为水平视图,同时获得列表的性能优势