2014-02-06 64 views
0

我有一個Uicollectionview,其中有800個單元格,當滾動或縮放時非常緩慢。預加載UICollectionViewCells

方法updateVisibleCellsNow大約需要9000毫秒,並且會降低App的速度。

我仍然設置

cell.layer.shouldRasterize = YES; 
cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 

但它並沒有在所有的工作。

所以我試圖緩存所有的單元格在viewDidLoad,緩存他們在一個NSCache中,並從cahce訪問它們,但它像我強迫用戶一個reuseIdentifiert,並不能從緩存中訪問創建的單元格。

有沒有解決這個問題的方法?

問候,

亞歷

回答

0

您創建細胞無標識,被加載你的觀點時,你應該加入這一行。

[self.collectionView registerClass:[AECalendarCell class] forCellWithReuseIdentifier:@"identifier"]; 
+0

我不保存圖像,只能用數據UITextfeild這是在viewWillAppear中loeaded。我用較少的單元測試它,這些單元需要較少的時間來加載,在背景中保存在nsarrays中的數據相同。 –

+0

你可以在創建單元格和單元類時添加代碼嗎? – bsarr007

0

我的自定義CollectionViewCell:

我.h文件中

@interface AECalendarCell : UICollectionViewCell 
{ 
    AEProjectDayItem *item; 
    NSIndexPath *thePath; 
    UILabel *lbl; 
} 
@property(nonatomic, strong) UILabel *lbl; 
@property(nonatomic, strong) NSIndexPath *thePath; 
@property(nonatomic, strong) AEProjectDayItem *item; 
@property (nonatomic) CGFloat scale; 
@property bool isAM; 
-(void)redrawCellWithString:(NSString*)String; 
@end 

我.m文件 @implementation AECalendarCell @synthesize LBL,項目,thePath;

-(id)initWithFrame:(CGRect)frame 
{ 
    if (self == [super initWithFrame:frame]) { 
     lbl = [[UILabel alloc]init]; 
    } 
    return self; 
} 
-(void)redrawCellWithString:(NSString*)String 
{ 
    [lbl removeFromSuperview]; 
    lbl.frame = self.contentView.bounds; 
    lbl.lineBreakMode = NSLineBreakByWordWrapping; 
    lbl.numberOfLines = 0; 
    if(self.scale >= 0.9) 
     lbl.font = [UIFont fontWithName:@"Arial" size:14]; 
    else if(self.scale >= 0.8) 
     lbl.font = [UIFont fontWithName:@"Arial" size:12]; 
    else if(self.scale >= 0.7) 
     lbl.font = [UIFont fontWithName:@"Arial" size:10]; 
    else if(self.scale >= 0.6) 
     lbl.font = [UIFont fontWithName:@"Arial" size:8]; 
    else if(self.scale >= 0.5) 
     lbl.font = [UIFont fontWithName:@"Arial" size:6]; 
    lbl.backgroundColor = [UIColor clearColor]; 
    lbl.textAlignment = NSTextAlignmentCenter; 
    if ([String isEqualToString:@""]) 
     lbl.text = @" "; 
    else 
     lbl.text = String; 
    lbl.textColor = [UIColor blackColor]; 
    if(thePath.section == 1 && thePath.item == 0) 
    { 
     CALayer *edgeBorder = [CALayer layer]; 
     [edgeBorder setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     [edgeBorder setFrame:CGRectMake(self.bounds.size.width-2, self.bounds.size.height-2, 2, 2)]; 
     [lbl.layer addSublayer:edgeBorder]; 
    } 
    if(thePath.section == 1 && thePath.item>0) 
    { 
     CALayer *bottomBorder = [CALayer layer]; 
     [bottomBorder setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     [bottomBorder setFrame:CGRectMake(0, self.bounds.size.height-2, self.bounds.size.width, 2)]; 
     [lbl.layer addSublayer:bottomBorder]; 
    } 
    if(thePath.section > 1 && thePath.item == 0) 
    { 
     CALayer *rightBorder = [CALayer layer]; 
     [rightBorder setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     [rightBorder setFrame:CGRectMake(self.contentView.bounds.size.width-2, 0, 2, self.contentView.bounds.size.width)]; 
     [lbl.layer addSublayer:rightBorder]; 
    } 
    if(thePath.section > 1 && thePath.row > 1 && thePath.row %2 == 0) 
    { 
     CALayer *endofDayLayer = [CALayer layer]; 
     [endofDayLayer setFrame:CGRectMake(self.frame.size.width-2, 0, 2, self.frame.size.width)]; 
     if(thePath.section % 2 == 0) 
      [endofDayLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     else 
      [endofDayLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
     [self.lbl.layer addSublayer:endofDayLayer]; 
    } 
    [self.contentView addSubview:lbl]; 
} 

-(void)prepareForReuse 
{ 
    [super prepareForReuse]; 
    lbl.layer.sublayers = NULL; 
    [lbl removeFromSuperview]; 
} 

添加單元格:

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
AECalendarCell *cell = [CV_plantabel dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath]; 
if(cell == nil) 
{ 
    cell = [[AECalendarCell alloc]init]; 
} 
cell.thePath = indexPath; 
cell.scale = scale; 
cell.layer.shouldRasterize = YES; 
cell.layer.rasterizationScale = [UIScreen mainScreen].scale; 
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init]; 
[DateFormatter setDateFormat:@"dd.MM.yyyy"]; 
NSDateFormatter *headerDateFormatter=[[NSDateFormatter alloc] init]; 
[headerDateFormatter setDateFormat:@"dd.MM"]; 
NSDate *today = [DateFormatter dateFromString:[DateFormatter stringFromDate:startDate]]; 

NSDate *headerDay; 
if(indexPath.section == 0) 
    headerDay = [today dateByAddingTimeInterval:60*60*24*(indexPath.row-1)]; 
else 
    headerDay = [today dateByAddingTimeInterval:60*60*12*(indexPath.row-1)]; 

if(indexPath.section==0) 
{ 
    cell.backgroundColor = [UIColor grayColor]; 
    if(indexPath.row>0) 
     [cell redrawCellWithString:[headerDateFormatter stringFromDate:headerDay]]; 
} 

else if(indexPath.section == 1) 
{ 
    cell.backgroundColor = [UIColor grayColor]; 
    if(indexPath.row == 0) 
     [cell redrawCellWithString:@"Name"]; 
    else 
    { 
     if(indexPath.row % 2 == 1) 
     { 
      [cell redrawCellWithString:@"AM"]; 
     } 
     else 
     { 
      [cell redrawCellWithString:@"PM"]; 
     } 
    } 
} 
else 
{ 
    [cell redrawCellWithString:@""]; 
    if(indexPath.row % 2 == 1) 
    { 
     cell.item = [[AEProjectDayItem alloc]initWithDate:headerDay andUser:[[[users objectAtIndex:indexPath.section-2]valueForKey:@"userID"]intValue] andisAM:YES]; 
    } 
    else 
    { 
     cell.item = [[AEProjectDayItem alloc]initWithDate:headerDay andUser:[[[users objectAtIndex:indexPath.section-2]valueForKey:@"userID"]intValue] andisAM:NO]; 
    } 
    //set Colors 
    if(indexPath.section % 2 == 0) 
    { 
     [cell setBackgroundColor:[UIColor grayColor]]; 
    } 
    else 
    { 
     [cell setBackgroundColor:[UIColor darkGrayColor]]; 
    } 
    //set Data 
    if(indexPath.item == 0) 
    { 
     if(self.currentScale >= 0.6) 
      [cell redrawCellWithString:[[users objectAtIndex:indexPath.section-2]valueForKey:@"name"]]; 
     else 
      [cell redrawCellWithString:[[users objectAtIndex:indexPath.section-2]valueForKey:@"nachname"]]; 
    } 

    //adjust row height 
    for(NSArray *item in projectDayItems) 
    { 
     if(indexPath.item>0) 
     { 
      if([[item valueForKey:@"datum"]isEqualToString:[DateFormatter stringFromDate:headerDay]] && 
       [[item valueForKey:@"AM"]boolValue] == cell.item.isAM && 
       [[item valueForKey:@"userID"]integerValue] == cell.item.theUserID && 
       cell.item != NULL) 
      { 
       cell.item.projectDayId = [[item valueForKey:@"projectDayID"]integerValue]; 
       cell.item.bereich = [item valueForKey:@"bereich"]; 
       cell.item.project = [[AEProject alloc]initwithID:[[item valueForKey:@"projectID"]integerValue] andName:[item valueForKey:@"projectname"]]; 
       cell.item.theClass = [item valueForKey:@"class"]; 
       cell.item.sequenceID = [[item valueForKey:@"sequence"]integerValue]; 
       [cell redrawCellWithString:cell.item.project.projectName]; 
      } 
      else 
      { 
       [cell redrawCellWithString:cell.item.project.projectName]; 
      } 
     } 
    } 
} 
//set the Accessibility Label 
if(cell.item != NULL && indexPath.section > 1 && indexPath.item>0) 
{ 
    NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init]; 
    [DateFormatter setDateFormat:@"EEEE, d. MMMM"]; 
    NSString *accString; 
    if(cell.item.project != nil) 
    { 
     if(cell.item.isAM) 
      accString = [NSString stringWithFormat:@"%@ Vormittag, %@ für %@", [DateFormatter stringFromDate:cell.item.theDate], cell.item.project.projectName, [[users objectAtIndex:indexPath.section-2]valueForKey:@"name"]]; 
     else 
      accString = [NSString stringWithFormat:@"%@ Nachmittag, %@ für %@", [DateFormatter stringFromDate:cell.item.theDate], cell.item.project.projectName, [[users objectAtIndex:indexPath.section-2]valueForKey:@"name"]]; 
     cell.lbl.accessibilityLabel = accString; 
    } 
    else 
    { 
     if(cell.item.isAM) 
      accString = [NSString stringWithFormat:@"%@ Vormittag für %@", [DateFormatter stringFromDate:cell.item.theDate], [[users objectAtIndex:indexPath.section-2]valueForKey:@"name"]]; 
     else 
      accString = [NSString stringWithFormat:@"%@ Nachmittag für %@", [DateFormatter stringFromDate:cell.item.theDate], [[users objectAtIndex:indexPath.section-2]valueForKey:@"name"]]; 
     cell.lbl.accessibilityLabel = accString; 
    } 
} 
//set Layout 
if(indexPath.row > 0 && indexPath.section > 1) 
    [self setLayoutforCell:cell]; 
[self setCell:cell forIndexPath:indexPath]; 
return cell; 
} 
+0

當你的視圖被加載時,你應該添加這行['self.collectionView registerClass:[AECalendarCell class] forCellWithReuseIdentifier:@「identifier」]''。 – bsarr007

+0

問題是:是否可以添加Cells WITHOTU identifyiert,因爲如果我使用reuseidentifier會導致性能不佳。 –