我有一个Uicollectionview,里面有大约800个单元格,当滚动或放大或缩小时非常慢。
方法updateVisibleCellsNow大约需要9000毫秒,并且会减慢App。
我仍然设置
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
但它根本不起作用。
所以我试图在viewDidLoad中缓存所有单元格,将它们缓存在NSCache中并从cahce访问它们,但它接缝像我被迫使用reuseIdentifiert并且无法从缓存中访问创建的单元格。
这个问题有解决办法吗?
问候,
亚历
答案 0 :(得分:0)
您创建没有标识符的单元格,您应该在加载视图时添加此行。
[self.collectionView registerClass:[AECalendarCell class] forCellWithReuseIdentifier:@"identifier"];
答案 1 :(得分:0)
MY Custom 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,item,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;
}