简体   繁体   中英

Improve CollectionView loading speed

I have a calendar view with a year overview:

我构建它看起来就像iOS日历

For that I have the following View hierarchy:

  • ViewController
    • View
      • Collection View with 12 cells (One for each month)
        • Colleciton View with 49 cells (One for each day + in and out dates + days of week)

Now when it comes to the following code part where I load the cells for the days inside the overview:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CalendarPageYearCollectionCellMonthCell;

    print("cell for item at: \(indexPath.item)");

    cell.layer.shouldRasterize = true;
    cell.layer.rasterizationScale = UIScreen.main.scale;

    cell.label.text = self.days[indexPath.item];

    return cell;
}

This code part takes over 9 seconds. Meanwhile the UI is frozen. How can I improve the loading and rendering speed of those cells significantly so that it wont take more than 1 second to load the view?

EDIT I found out which part of the code is consuming this amount of time. I created a UICollectionViewCell programmatically:

import Foundation
import UIKit
import SnapKit

class CalendarPageYearCollectionCellMonthCell : UICollectionViewCell {

    var label: UILabel! = nil;

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

        self.setupView();
    }

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

    func setupView() {
        self.backgroundColor = UIColor.green;

        self.label = UILabel();
        self.label.font = UIFont(name: "HelveticaNeue", size: 12);
        self.label.textColor = PaintBox.colorWithName(VKBColor.black, andAlpha: 1.0);
        self.label.backgroundColor = UIColor.red;
        self.label.textAlignment = .center;
        self.sizeToFit();
        self.label.center = self.center;

        self.addSubview(self.label);
    }
}

So when I use this code the Calendar loads really fast. But with that code, ofcourse, you will never see the label which you added as a subview. So I need to add some constraints:

func setupView() {
    self.backgroundColor = UIColor.green;

    self.label = UILabel();
    self.label.font = UIFont(name: "HelveticaNeue", size: 12);
    self.label.textColor = PaintBox.colorWithName(VKBColor.black, andAlpha: 1.0);
    self.label.backgroundColor = UIColor.red;
    self.label.textAlignment = .center;
    self.sizeToFit();
    self.label.center = self.center;

    self.addSubview(self.label);

    self.label.snp.makeConstraints { make in
        make.edges.equalToSuperview();
    }
}

And now it takes ages for the view to load. So the real problem seems to be the constraints.

Isnt there a ways to just create a cell only one time and copy that to all other cells with just setting the text?

So after lot of researching I came to the conclusion that I cannot speed this up while using storyboards and constraints. It is always slow as hell.

So I tried to get rid of all xib files, storyboard files and constraints. The result was incredible. I made it from a view loading speed of 9 seconds to a loading speed of 230 ms.

So what I recommend to everybody: If you have performance issues just dont use xib's, storyboard's or constraints. Just code everything yourself and set all frames of your views directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM