ios - 制作日历。年视图真的很慢。很多子视图

时间:2015-02-10 19:51:23

标签: ios performance cocoa-touch uicollectionview uicollectionviewcell

我正在制作一个日历,尤其是年份视图,并且可以理解的是,在进入视图时速度很慢。

年视图只是一个UICollectionView,每个月都是一个自定义的UICollectionViewCell XIB。这个月有一个标题UILabel,7天名称UILabels和42天数UILabels。

每月总计50 UILabels X 12. Aieesh ...

其他iOS日历应用如何处理显示如此多的子视图而不降低性能?性能受欢迎主要是在将此视图刷到下一年和前几年时。当视图滑入时,集合视图正在传播单元格,从而导致减速。我可以推迟填充单元格,直到滚动停止,但我真的不想显示白色视图,因为它滚动到窗口。

编辑*根据要求 - 我的cellForRow方法:

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

    NSIndexPath *indexPathPlusOne = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];

    EICalendarYearViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EICalendarYearViewCell" forIndexPath:indexPath];

    if (_datesLoaded == YES)
    {
        //Get First Day in Month
        NSMutableArray *arrayForDatesInMonth = [_dictHoldingArrayForEachMonth objectForKey:[NSNumber numberWithLong:indexPathPlusOne.row]];
        NSDate *firstDayInMonth              = arrayForDatesInMonth[0];
        NSDateComponents* comp               = [[NSCalendar currentCalendar] components:(NSCalendarUnitWeekday)  fromDate:firstDayInMonth];

        //setting month label
        switch (indexPathPlusOne.row) {
            case 1:
                cell.labelTitle.text = @"January";
                break;

            case 2:
                cell.labelTitle.text = @"February";
                break;

            case 3:
                cell.labelTitle.text = @"March";
                break;

            case 4:
                cell.labelTitle.text = @"April";
                break;

            case 5:
                cell.labelTitle.text = @"May";
                break;

            case 6:
                cell.labelTitle.text = @"June";
                break;

            case 7:
                cell.labelTitle.text = @"July";
                break;

            case 8:
                cell.labelTitle.text = @"August";
                break;

            case 9:
                cell.labelTitle.text = @"September";
                break;

            case 10:
                cell.labelTitle.text = @"October";
                break;

            case 11:
                cell.labelTitle.text = @"November";
                break;

            case 12:
                cell.labelTitle.text = @"December";
                break;

            default:
                break;
        }

        //setting number of days in previous month to show.
        long previousMonthDays = 0;
        if (comp.weekday == 7)
        {
            previousMonthDays = 0;
        }
        else
        {
            previousMonthDays = comp.weekday - 1;
        }


        for (int i = 0; i < cell.arrayOfDayLabels.count; i++)
        {
            UILabel *labelDay = cell.arrayOfDayLabels[i];

            //previous Month days
            if (i <= previousMonthDays)
            {
                labelDay.text = @" ";
                labelDay.textColor = [UIColor colorWithWhite:0.8 alpha:1.000];
            }

            //Actual days in current month
            if (i >= previousMonthDays && i < previousMonthDays + arrayForDatesInMonth.count)
            {
                NSDate *dateForDay = arrayForDatesInMonth[i - previousMonthDays];
                NSDateComponents *compsForDay = [[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:dateForDay];
                labelDay.text = [NSString stringWithFormat:@"%ld", (long)compsForDay.day];
                labelDay.textColor = [UIColor darkGrayColor];
            }
            else
            {
                labelDay.text = @" ";
                labelDay.textColor = [UIColor colorWithWhite:0.8 alpha:1.000];
            }
        }
    }
    else
    {
        for (int i = 0; i < cell.arrayOfDayLabels.count; i++)
        {
            UILabel *labelDay = cell.arrayOfDayLabels[i];
            labelDay.text = @" ";
        }
    }

    cell.layer.shouldRasterize = YES;
    [cell.layer setRasterizationScale:3.0f];

    return cell;
}

对于rmaddy,这些方法的屏幕截图导致大部分速度减慢。

enter image description here

0 个答案:

没有答案