objective-c - UICollectionView 可滚动年 View : performance issue

标签 objective-c performance uicollectionview

我目前正在处理一个困扰我很多的问题。我正在开发一个无休止的可滚动年 View ,它与标准的苹果日历应用程序非常相似:

enter image description here

所以我基本上有 2 个数组,第一个包含月份第一天的相应工作日,第二个包含我要显示的月份的总天数。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {


    int currentYearIndex = (int)indexPath.section * 12;

    int fillDays      = [[self.dayIndexThisMonthArray objectAtIndex:indexPath.row  + currentYearIndex ]intValue];
    int daysThisMonth = [[self.daysThisMonthArray     objectAtIndex:indexPath.row  + currentYearIndex ]intValue];

    EMYearCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"YearCell" forIndexPath:indexPath];
    [cell configureForNumberOfFillDays: fillDays daysThisMonth: daysThisMonth];

    return cell;

}

我创建了一个自定义的 UICollectionView 类,它接收上述 2 条信息并在创建相应月份后进行处理:

#import "EMYearCell.h"

@interface EMYearCell()

@end

@implementation EMYearCell


- (void)configureForNumberOfFillDays: (int)fillDays daysThisMonth: (int)daysThisMonth
{
    @autoreleasepool {

        void (^labelingBlock) (int, int, int)= ^(int horizontalSpacingMultiplier, int i,  int fillDayAdjuster){

            self.theLabel = [[EMCalendarLabel alloc]initWithFrame:CGRectMake(self.initialXCord + i * self.distanceBetweenLabels, self.initialYCord + self.horizontalSpacing * horizontalSpacingMultiplier, 50, 50)];
            [self.theLabel setText:[NSString stringWithFormat:@"%d",i + fillDayAdjuster - fillDays]];
            [self addSubview:self.theLabel];
        };

    for (int i = fillDays; i < 7; i++){

        labelingBlock (1,i,1);
    }

    for (int i = 0; i < 7; i++){

        labelingBlock (2,i,8);
    }

    for (int i = 0; i < 7; i++){

        labelingBlock (3,i,15);
    }

    for (int i = 0; i < 7; i++){

        labelingBlock (4,i,22);
    }

    for (int i = 0; i < 7; i++){

        if (i + 29 - fillDays < daysThisMonth + 1){

            labelingBlock (5,i,29);
        }
    }

    for (int i = 0; i < 7; i++){

        if (i + 36 - fillDays < daysThisMonth + 1){

                labelingBlock (6,i,36);
            }
        }
    }
}

尽管我只是将一堆标签作为 subview 添加到我的自定义 UICollectionView 单元格中,但滚动非常缓慢且不稳定。我真的想不出有什么可能让这一切变得更便宜。

我试过这个:

[self.view.layer setShouldRasterize:YES];
[self.view.layer setRasterizationScale:[UIScreen mainScreen].scale];

但我也没有发现任何改善。

我非常感谢任何可以帮助我解决这种情况的建议。

最佳答案

你可以简单地做这样的事情:

@interface EMYearCell : UICollectionViewCell    
@property (nonatomic, strong) NSOperation *updateOperation;
@end

@interface MyCollectionViewController ()
@property NSOperationQueue *operationQueue;
@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.operationQueue = [NSOperationQueue new];        
    self.operationQueue.maxConcurrentOperationCount = 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        EMYearCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"YearCell" forIndexPath: indexPath];

        // Increment the cell's tag
        NSInteger currentTag = cell.tag + 1;
        cell.tag = currentTag;

        NSBlockOperation *updateOperation = [NSBlockOperation blockOperationWithBlock:^{

                // preparing data for the cell here

                dispatch_sync(dispatch_get_main_queue(), ^{
                        // Only update the cell if the cell tag hasn't changed. Otherwise, the cell has been re-used.
                        if (cell.tag == currentTag) {

                                // update the cell here

                        }
                });
        }];

        cell.updateOperation = updateOperation;
        [self.operationQueue addOperation:updateOperation];

        return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
        // cancel the associated update operation
        EMYearCell *yearCell = (EMYearCell *)cell;
        [yearCell.updateOperation cancel];
}

关于objective-c - UICollectionView 可滚动年 View : performance issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197893/

相关文章:

javascript - 将递归 JS 函数转换为 Obj-C

java - 对机器人进行编程以探索网格

performance - 这个质数相关谓词的瓶颈是什么?

ios - 如何在保持单元格选择的同时向 UICollectionView 添加点击手势?

ios - 具有自定义布局的 UICollectionView 布局

ios - 为UIImageView中的图像提供特效

javascript - 如何使用 Objective C 或 javascript 在 cydget 中运行系统命令?

ios - 为什么在应用程序的初始启动期间不显示照片?

objective-c - UIImagePickerController 在 iOS 8 中没有正确呈现

sql - 在数据库中存储现实世界 "events"的最佳方式?