max*_*ax_ 5 performance objective-c lag uitableview ios
我有以下代码来添加边框颜色和阴影到我的UITableViewCell的背景.我的问题是这段代码会导致tableView本身出现大量延迟.
请问您如何优化我的代码,防止UITableView的延迟?
if ([cell viewWithTag:012] == nil && comment.isReply == NO) {
UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease];
[iv setImage:[UIImage imageNamed:@"paper"]];
[iv setTag:012];
[cell insertSubview:iv atIndex:0];
[iv.layer setBorderWidth:1.0];
[iv.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[iv.layer setShadowColor:[[UIColor blackColor] CGColor]];
[iv.layer setShadowOffset:CGSizeMake(0, 1)];
[iv.layer setShadowOpacity:0.75];
}
else if ([cell viewWithTag:012] == nil && comment.isReply == YES) {
frame.origin.x += 35;
UIImageView *iv = [[[UIImageView alloc] initWithFrame:frame] autorelease];
[iv setImage:[UIImage imageNamed:@"paper"]];
[iv setTag:012];
[cell insertSubview:iv atIndex:0];
UIImage *arrow = [UIImage imageNamed:@"arrow"];
UIImageView *ivs = [[[UIImageView alloc] initWithFrame:CGRectMake(-12, ([cell frame].size.width / 2) + ([arrow size].width/2) , arrow.size.width, arrow.size.height)] autorelease];
[cell addSubview:ivs];
[iv.layer setBorderWidth:1.0];
[iv.layer setBorderColor:[[UIColor whiteColor] CGColor]];
[iv.layer setShadowColor:[[UIColor blackColor] CGColor]];
[iv.layer setShadowOffset:CGSizeMake(0, 0)];
[iv.layer setShadowOpacity:0.75];
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*ams 20
除了这里的其他优化建议之外,shadowPath
在您的CALayer
意志上指定一个可以改善阴影绘制性能.你可以用这样的东西确定阴影的路径......
iv.layer.shadowPath = [UIBezierPath bezierPathWithRect:iv.bounds].CGPath;
Run Code Online (Sandbox Code Playgroud)
您可能还想查看shouldRasterize
CALayer上的位.这会导致图层预渲染为位图.如果您选择此路线,请务必提供与您的设备匹配的光栅尺.
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
Run Code Online (Sandbox Code Playgroud)
您应该避免在每次加载时操作单元格,而应该在初始化/创建单元格时调整 UI。
为了说明这一点,每次滚动时都可以使用该方法加载一个新单元格(或多个单元格)cellForRowAtIndexPath:
,目前您正在此方法中进行大量视图更改,但在某些情况下可能不需要这样做(例如新单元格与刚刚滚出屏幕的类型相同)。将此 UI 修改移动到单元格初始化的位置,而不是交换数据的位置。您可以使用子类或像这样简单地执行此操作。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Reuse id
static NSString *identifier1 = @"identifer-1";
static NSString *identifier2 = @"identifier-2";
static NSString *regular = @"regular";
UITableViewCell *cell;
if (comment.isReply == NO) {
cell = [tableView dequeueReusableCellWithIdentifier: identifier1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier1] autorelease];
// Do the UI modification here
}
} else if (comment.isReply == YES) {
cell = [tableView dequeueReusableCellWithIdentifier: identifier2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: identifier2] autorelease];
// Do the UI modification here
}
} else {
// Regular cell
cell = [tableView dequeueReusableCellWithIdentifier: regular];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: regular] autorelease];
}
}
// Load the data into the cell
return cell;
}
Run Code Online (Sandbox Code Playgroud)
希望你能明白我的意思,关键是尽可能少做繁重的事情,让 UITableView 缓存发挥更大的作用。
归档时间: |
|
查看次数: |
4513 次 |
最近记录: |