简体   繁体   中英

UICollectionView in UICollectionViewCell slow/laggy scrolling

I am trying to create a calendar view same as iOS but only for four years.

The hierarchy is like: UICollectionView: One Section for each year UICollectionViewCell: 12 cells/items for 12 months in an year UICollectionView: UICollectionViewCell: Upto 31 cells for month day string, which have UILabel as their content.

Screenshot attached.

在此处输入图片说明

In order to get over the laggy scrolling, I removed the datasource of Month cells. The container view controller is itself the data source for both collectionviews.

The 12 month collection view has been subclassed to store the index path for year value. Got this trick from this tutorial: https://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/

But issue was still there as I was calling reloadData on month cell before displaying it.

To fix this I did override the prepareForReuse() method of the UICollectionViewCell and set the indexPath, delegate and datasource properties to nil in the monthCollectionViewCell. Also, removed the reloadData call. But the scrolling is still laggy.

The tutorial link is pretty much same as my workflow.

Here is the link to my code: https://github.com/nipun0505/TestCalendarView

You can reduce lag by:

1) Caching your colors.

Try commenting out this line of code and see how better the lag is:

//        if ([[_dateCompareFormatter stringFromDate:date] isEqualToString:[_dateCompareFormatter stringFromDate:[NSDate date]]])
//        {
//            dayCell.dayLabel.backgroundColor = [UIColor cyanColor];
//        }

//        else
//        {
//            dayCell.dayLabel.backgroundColor = [UIColor clearColor];
//        }

After caching your colors, keep in mind that clear colors, will lag more than opaque colors.

2) Why are you creating a date formatter over and over again in this function?

(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
NSDateFormatter *df = [[NSDateFormatter alloc] init];

Please cache stuff like this. Caching is your friend.

3) Performance can also be increased in other areas. Your calendar load time is horrible :s .

If you speed this up, and do the above 2 things, you will see a noticeable increase in speed.

[Edit to #3] --> What i mean is Im saying that the load time for your calendar is almost 4 seconds. This is because you are displaying many items on the screen. Maybe you can change your design a bit? Here's a crazy idea. For every month, your calendar currently has to keep track of around 30 items. That's about 30 items per month for about 15 months per iPhone 6s screen. Instead of keeping track of 30 items, maybe keep track of 6 labels arranged in a row of a MonthCell? You will have to use some calculation to get the text aligned, but your performance boost will be incredible if you can get it to work.

I see you are having a blue colored back view. So can have two types of cells. One with the 6 labels, and another with the 30 items. You can present which cell based on your needs. There are many things you can cook-up to increase performance.

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