1

我使用以下代码在我的 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 display”iPad 时,也会出现这种闪烁。也许这与点/像素差异有关?

4

1 回答 1

3

我自己发现了这个错误。对不起,打扰你。以防其他人遇到同样的问题: contentOffset.x 是一个浮点值,包含 0.5 用于 Retina 显示器的值。将其分配给 int 变量会layoutSubviews导致问题。这是修复(注意我用 '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)];
    }
}
于 2012-10-15T17:47:34.507 回答