2016-01-15 2 views
12

私はUICollectionViewCellのサブクラスにUILabelsのレイアウトを使用しています。私はiOS SDKを使用しています9.2UICollectionViewCells内のUIStackViewのパフォーマンスが悪い

デキュー時にラベル 'textを更新しないと、コレクションビューのスクロールがスムーズになります。しかし、それらをデキューするときにtextを更新すると、スクロールが非常に遅くなります。

私は問題を表示するために、シミュレータではなくデバイス上で実行するための非常に小さなデモを作成しました。あなたは、新しい空のプロジェクトを作成し、これをViewController.swiftの内容を置き換えることができます。

import UIKit 

class ViewController: UIViewController { 

    override func loadView() { 
     view = UIView() 

     let layout = UICollectionViewFlowLayout() 
     layout.itemSize = CGSize(width: 100, height: 200) 
     let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout) 
     collectionView.registerClass(Cell.self, forCellWithReuseIdentifier: "Cell") 
     collectionView.translatesAutoresizingMaskIntoConstraints = false 
     collectionView.dataSource = self 
     view.addSubview(collectionView) 

     let constraints = ["H:|-[collectionView]-|", 
      "V:|[collectionView]|" 
     ].flatMap { NSLayoutConstraint.constraintsWithVisualFormat($0, options: [], metrics: nil, views: ["collectionView": collectionView]) 
     } 
     NSLayoutConstraint.activateConstraints(constraints) 

    } 
} 

extension ViewController: UICollectionViewDataSource { 
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! Cell 

     //comment out the line below to make the scrolling smoother: 
     cell.fillLabels() 

     return cell 
    } 
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 100 
    } 
} 

class Cell: UICollectionViewCell { 

    var labelArray = [UILabel]() 

    func fillLabels() { 
     for label in labelArray { 
      label.text = "\(label.text!) yo" 
     } 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 

     contentView.backgroundColor = UIColor.whiteColor() 

     let stackView = UIStackView() 
     stackView.axis = .Horizontal 
     stackView.alignment = .Leading 
     stackView.distribution = .EqualSpacing 
     stackView.translatesAutoresizingMaskIntoConstraints = false 
     contentView.addSubview(stackView) 

     let leftStack = UIStackView() 
     leftStack.axis = .Vertical 

     let rightStack = UIStackView() 
     rightStack.axis = .Vertical 

     stackView.addArrangedSubview(leftStack) 
     stackView.addArrangedSubview(rightStack) 

     for index in 0...10 { 
      let leftLabel = UILabel() 
      leftLabel.text = "\(index)" 
      leftStack.addArrangedSubview(leftLabel) 

      labelArray.append(leftLabel) 

      let rightLabel = UILabel() 
      rightLabel.text = "\(index)" 
      rightStack.addArrangedSubview(rightLabel) 

      labelArray.append(rightLabel) 
     } 


     let constraints = [ 
      "H:|[stackView]|", 
      "V:|[stackView]|" 
      ].flatMap { 
       NSLayoutConstraint.constraintsWithVisualFormat($0, options: [], metrics: nil, views: ["stackView": stackView]) 
     } 

     NSLayoutConstraint.activateConstraints(constraints) 

    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

あなたはfillLabelsへの呼び出しをコメントアウトしたときにスクロールがスムーズでわかります。

UIStackViewsを付けずに同じレイアウトを再現しようとすると、fillLabelsという呼び出しが含まれていると、スクロールもスムーズになります。

これは、レイアウトを再計算すると、UIStackViewがパフォーマンスのボトルネックを被ることを示しています。

この仮説は正しいですか?いくつかのソリューションがありますか?

+0

こんにちはエリック!あなたはこれについて何か結論を見つけることができましたか? – damirstuhec

+1

いいえ、バグフィール:https://openradar.appspot.com/24206043 – Eric

+0

UICollectionViewの属性インスペクタでプリフェッチオプションが有効になっていますか – Balanced

答えて

0

自動レイアウトを使用してみましたか?私の経験では、自動レイアウトは多くの状況でパフォーマンスの犯人です。

translatesAutoresizingMaskIntoConstraintsをfalse(デフォルトはtrue)に設定しないで、コレクションビュー自体の制約ベースのレイアウトを削除しないでください(代わりに自動サイズ変更マスクを使用してください)。

関連する問題