2

セルが次のように見えるこのテーブルがあります UITableViewCell
。現在、この境界線は背景の一部です (今のところ重なりはありません)。

これで、境界線を背景から分離し、セル作成コードは次のようになります。

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

問題は、テーブルを上にスクロールしているときにのみ境界線が表示されることです。それ以外の場合、境界線自体がその上のセルに重なっています。(間違った方法で。)

セルが要求されるたびに境界線 ( ) を追加して(これは明らかに複数の描画につながり、見栄えがよくありません。それでも問題は解決しません)を試し[cell setNeedsDisplay]ました。-tableView:willDisplayCell:forRowAtIndexPath:cell-top.pngimg.layer.zPosition = 2

その UIImageView を常にトップに保つにはどうすればよいですか?

4

1 に答える 1

9

私の提案は、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 をターゲットにしている場合の別のオプションは、代わりに z オーダーのサポートが組み込まれている UICollectionView を使用することです。

于 2012-04-15T21:32:48.833 に答える