重叠UITableViewCell内容视图 [英] Overlapping UITableViewCell content views

查看:128
本文介绍了重叠UITableViewCell内容视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表格,其单元格如下所示:


如您所见,每个单元格的顶部边框应与上面的单元格重叠。现在,这个边框是背景的一部分(到目前为止没有重叠)。

I have this table whose cells look like this:
As you can see, the top border of every cell is supposed to overlap with the cell above. Right now, this border is part of the background (no overlapping so far).

现在,我将边框与背景分离,我的单元格创建代码看起来像this:

Now, I've separated the border from the background and my cell creation code looks like this:

#define QAImage(NAME) [[[UIImageView alloc] initWithImage:[UIImage imageNamed:NAME]] autorelease]

- (UITableViewCell*) tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)index
{
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:@"Entry"];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                         reuseIdentifier:@"Entry"] autorelease];
        cell.backgroundView = QAImage(@"bg-cell.png");
        cell.selectedBackgroundView = QAImage(@"bg-cell-sel.png");
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        UIImageView *img = QAImage(@"disclosure.png");
        img.highlightedImage = [UIImage imageNamed:@"disclosure-sel.png"];
        cell.accessoryView = img;
        CGRect r = cell.frame;
        r.size.height = table.rowHeight;
        r.size.width = table.frame.size.width;
        cell.frame = r;
        r = cell.contentView.frame;
        r.origin = CGPointMake(13, 13);
        r.size.width -= 8 + img.frame.size.width;
        r.size.height -= 25;
        [cell.contentView addSubview:[[[TagView alloc] initWithFrame:r] autorelease]];
        img = QAImage(@"cell-top.png");
        img.highlightedImage = [UIImage imageNamed:@"cell-top-sel.png"];
        img.opaque = NO;
        r = img.frame;
        r.origin = CGPointMake(0, 1 - r.size.height); // The (supposed) overlapping happens here.
        img.frame = r;
        cell.contentView.clipsToBounds = NO;
        [cell.contentView addSubview:img];
    }
    // Here I set the title. (omitted)
    return cell;
}

问题是在表格中向上滚动,否则它本身与其上方的单元格重叠。 (错误的方式。)

The problem is, the border is only shown when I'm scrolling up in the table, else it is itself overlapped by the cell above it. (The wrong way around.)

我试过 [cell setNeedsDisplay] -tableView:willDisplayCell:forRowAtIndexPath:,每次请求单元格时添加边框( cell-top.png )绘图和看起来不好;不管,它仍然不能解决我的问题)和设置 img.layer.zPosition = 2

I've tried [cell setNeedsDisplay] in -tableView:willDisplayCell:forRowAtIndexPath:, adding the border (cell-top.png) every time a cell is requested (which obviously leads to multiple drawing and doesn't look good; regardless, it still doesn't solve my problem) and setting img.layer.zPosition = 2.

如何强制UIImageView一直保持在顶端?

How can I force that UIImageView to stay on top all the time?

推荐答案

子类UITableView和重写layoutSubviews,将可见单元格视图放入你想要它们的z顺序像这样:

My suggestion would be to subclass UITableView and override layoutSubviews to put the visible cell views into the z-order you want them to be in like this:

- (void)layoutSubviews {
    [super layoutSubviews];
    NSArray *sortedIndexPaths = [[self indexPathsForVisibleRows] sortedArrayUsingSelector:@selector(compare:)];
    for (NSIndexPath *path in sortedIndexPaths) {
        UITableViewCell *cell = [self cellForRowAtIndexPath:path];
        [self bringSubviewToFront:cell];
    }
}

这可能太慢,改变 bringSubviewToFront: sendSubviewToBack:,但希望给你一个方向尝试。

This might be too slow, or you might need to change bringSubviewToFront: to sendSubviewToBack:, but hopefully that gives you a direction to try out.

编辑:现在,另一个选项,如果你的目标是iOS 6,是使用一个UICollectionView,它已经内置支持z顺序。

Another option now, if you're targeting iOS 6, is to use a UICollectionView instead, which has built-in support for z-ordering.

这篇关于重叠UITableViewCell内容视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆