我使用以下代码在我的UIScrollView后代中生成“固定”行标题。它适用于模拟器,但不幸的是,它在iPad上闪烁。 (在行标题视图的左侧,出现白色1px行并消失。)可以改进哪些内容?
- (void)initSubviews
{
const int ROW_COUNT = 20;
rowHeaderViews = [[NSMutableArray alloc]initWithCapacity:ROW_COUNT];
rowViews = [[NSMutableArray alloc]initWithCapacity:ROW_COUNT];
[self setContentSize:CGSizeMake(2000, [self frame].size.height)];
for (int i = 0; i < ROW_COUNT; i++)
{
UIView *header = [self createRowHeaderViewForRowNum:i];
[rowHeaderViews addObject:header];
UIView *row = [self createRowViewForRowNum:i];
[rowViews addObject:row];
[self addSubview:row];
[self addSubview:header];
}
[self layoutSubviews];
}
- (void)layoutSubviews
{
int x = [self contentOffset].x;
for (UIView *view in rowHeaderViews) {
[view setFrame:CGRectMake(x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
}
}
- (UIView *)createRowHeaderViewForRowNum: (int)rowNum
{
UILabel *view = [[UILabel alloc]initWithFrame:CGRectMake(0, rowNum * 20, 200, 20)];
[view setBackgroundColor:[UIColor colorWithWhite:0.8*(20-rowNum)/20 alpha:1]];
[view setText:[NSString stringWithFormat:@"Row Header %d", rowNum]];
return view;
}
- (UIView *)createRowViewForRowNum: (int)rowNum
{
UILabel *view = [[UILabel alloc]initWithFrame:CGRectMake(200, rowNum * 20, 1800, 20)];
[view setBackgroundColor:[UIColor colorWithRed:rowNum/20.0 green:0 blue:0 alpha:1]];
[view setText:[NSString stringWithFormat:@"Row Content %d", rowNum]];
return view;
}
非常感谢您的帮助!
编辑:iPad上有Retina显示屏。将模拟器与“普通”iPad一起使用时,它不会闪烁。将模拟器切换到“Retina显示器”iPad时,也会出现这种情况。也许这是关于点/像素差异的东西?
答案 0 :(得分:2)
我自己发现了这个错误。对不起,打扰你。
如果其他人遇到同样的问题:
contentOffset.x是一个浮点值,包含Retina显示的.5值。
将它分配给layoutSubviews
中的int变量会导致问题。
这是修复(注意我用'CGFloat'替换'int'):
- (void)layoutSubviews
{
CGFloat x = [self contentOffset].x;
for (UIView *view in rowHeaderViews) {
[view setFrame:CGRectMake(x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)];
}
}