【问题标题】:Overlapping UITableViewCell content views重叠 UITableViewCell 内容视图
【发布时间】:2012-04-15 21:11:50
【问题描述】:

我有一张表格,其单元格如下所示:
如您所见,每个单元格的顶部边框都应该与上面的单元格重叠。现在,这个边框是背景的一部分(目前没有重叠)。

现在,我已将边框与背景分开,我的单元格创建代码如下所示:

#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;
}

问题是,只有当我在表格中向上滚动时才会显示边框,否则它本身会被其上方的单元格重叠。 (错误的方式。)

我在-tableView:willDisplayCell:forRowAtIndexPath: 中尝试过[cell setNeedsDisplay],每次请求一个单元格时都添加边框(cell-top.png)(这显然会导致多次绘图并且看起来不太好;无论如何,它仍然没有解决我的问题)并设置img.layer.zPosition = 2

如何强制 UIImageView 始终保持在顶部?

【问题讨论】:

    标签: iphone ios cocoa-touch uikit


    【解决方案1】:

    我的建议是继承 UITableView 并覆盖 layoutSubviews 以将可见单元格视图放入您希望它们处于的 z 顺序中:

    - (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:,但希望这能给您一个尝试的方向。

    编辑:如果您的目标是 iOS 6,现在的另一个选择是使用 UICollectionView,它内置了对 z-ordering 的支持。

    【讨论】:

    • 非常感谢您的精彩提示!
    • Jesse 的代码中有几个错别字。我修改为: NSArray *sortedIndexPaths = [[self indexPathsForVisibleRows] sortedArrayUsingSelector:@selector(compare:)]; for (NSIndexPath *path in sortedIndexPaths) { UITableViewCell *cell = [self cellForRowAtIndexPath:path]; [自我带来SubviewToFront:细胞]; }
    • @BenBoral 感谢您的错字更正!我已将其集成到答案中。将来,当您看到这样的错别字时,您应该随时“建议编辑”帖子。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多