2014-02-06 10 views
0

У меня есть Uicollectionview с около 800 ячейками в нем, что очень медленно при прокрутке или масштабировании или выходе.Preload UICollectionViewCells

Метод updateVisibleCellsNow занимает около 9000 мс и замедляет приложение.

Я до сих пор установить

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

Но это не работает.

Итак, я попытался кэшировать все ячейки в viewDidLoad, кешировать их в NSCache и получать к ним доступ из cahce, но он сглаживается, как будто я вынужден использовать повторное использованиеIdentifiert и не может получить доступ к созданным ячейкам из кеша ,

Есть ли решение проблемы?

поздравления,

Alex

ответ

0

Вы создаете ваши клетки без идентификатора, вы должны добавить эту строку, если ваш вид загружается.

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

Я не сохранять изображения, только UITextfeild с данными, которые loeaded в viewWillAppear. Я тестировал его с меньшим количеством ячеек, которое занимает меньше времени для загрузки, с теми же данными в фоновом режиме, которые сохраняются в 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]; 
} 

Добавление Cell:

-(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] forCellWithReuseIdentifier: @" identifier "]'. – bsarr007

+0

Вопрос: можно ли добавить ячейки WITHOTU идентификатор, потому что есть плохая производительность, если я использую reuseidentifier. –

Смежные вопросы