2009年9月25日金曜日

iphone tableView グループCellの背景変更

TableView の Cell の背景を変更するとき、 普通に


cell.contentView.backgroundColor = [UIColor color] 」

とやっても、うまくいかない。角まで塗られてしまったり、Cellの周りの淵だけ塗られたりなど、散々。


そこで、これ!


João Prado Maia  さんのところにありました。

ライブラリはこれ

CustomCellBackgroundView *bgView = [[CustomCellBackgroundView alloc] initWithFrame:CGRectZero];
    if (indexPath.row == 0) {
        bgView.position = CustomCellBackgroundViewPositionTop;
    } else if (indexPath.row == count) {
        bgView.position = CustomCellBackgroundViewPositionBottom;
    } else {
        bgView.position = CustomCellBackgroundViewPositionMiddle;
    }


ライブラリの「CustomCellBackgroundView」のインスタンスを生成し、あとは、cellのポジションを指定。


Cellの一番上は、上が丸まるし、一番下は、下が丸まる。それ以外の所は丸まらないので、ポジションを指定する必要がある。



シングルの場合は、ライブラリにシングル用の処理を追加する。

(すでにインターフェースには変数の宣言はしてあるので、メソッドへの追加のみ

     } else if (position == CustomCellBackgroundViewPositionSingle) {
        CGFloat minx = CGRectGetMinX(rect) , midx = CGRectGetMidX(rect), maxx = CGRectGetMaxX(rect) ;
        CGFloat miny = CGRectGetMinY(rect) , midy = CGRectGetMidY(rect) , maxy = CGRectGetMaxY(rect) ;
        minx = minx + 1;
        miny = miny + 1;
          
        maxx = maxx - 1;
        maxy = maxy - 1;
      
        CGContextMoveToPoint(c, minx, midy);
        CGContextAddArcToPoint(c, minx, miny, midx, miny, 10.0f);
        CGContextAddArcToPoint(c, maxx, miny, maxx, midy, 10.0f);
        CGContextAddArcToPoint(c, maxx, maxy, midx, maxy, 10.0f);
        CGContextAddArcToPoint(c, minx, maxy, minx, midy, 10.0f);
          
        // Close the path
        CGContextClosePath(c);
        // Fill & stroke the path
        CGContextDrawPath(c, kCGPathFillStroke);              
        return;


で、肝心な色とボーダーの指定は、

    bgView.fillColor = [UIColor blackColor];
    bgView.borderColor = [UIColor whiteColor];

最後に
    cell.backgroundView = bgView;

0 件のコメント:

コメントを投稿