Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

ios - UITableView comes to a crawl when adding a UILabel with cornerRadius to each cell

I'm adding a UILabel to each cell in my table view. This presents no problem initially. When I round the corners of the UILabel using layer.cornerRadius scrolling the table view grinds to a halt.

 UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
 label1.backgroundColor = ([[managedObject valueForKey:@"color"] hasPrefix:@"FFFFFF"]) ? [UIColor blackColor] : color;
 label1.layer.cornerRadius = 10.0f;
 [cell addSubview:label1];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no need for an image with rouned corners - see the answer by fknrdcls to this question:

UILabel layer cornerRadius negatively impacting performance

Basically, you simply need to give your label a transparent background, and add the backgroundcolor to the layer instead. Then, you can disable maskToBounds, which greatly improves performance. So your code becomes:

UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(cell.bounds.origin.x+10 ,5, 30, 30)];
label1.backgroundColor = [UIColor clearColor];
label1.layer.backgroundColor=[UIColor whiteColor].CGColor;
label1.layer.cornerRadius = 10.0f;
label1.layer.masksToBounds = NO;
label1.layer.shouldRasterize = YES;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...