UITableView でデフォルトで提供される標準の水平線の上に垂直線を上書きすることにより、UITableView を使用してグリッド スタイルのテーブルを作成しようとしています。このブログ ( http://www.iphonedevx.com/?p=153 ) から役立つように提供された例にコードを適応させています。
まず、垂直線 (水平線と同じ色) を描画するコードは、「MyTableCell.m」と呼ばれるテーブル ビュー コントローラーとは別のファイルに実装されます。
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
次に、Table View Controller の tableView メソッドで、[cell addColumn:50] を呼び出して、ビュー境界の左側から 50 ピクセル左に垂直線を描画します。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
ビュー全体の背景を白 (デフォルト) から黒に変更しようとしています。Table View Controller の viewDidLoad メソッドで self.view.backgroundColor を黒に設定して、これを実行しようとしています。
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
}
しかし、これを行うと、縦線が消えます...
viewDidLoad で backgroundColor を設定すると、drawRect: メソッドに到達するまでに CurrentContext が変更されると思いますが、それを調整する方法がわかりません。drawRect 内の CGContextSetFillColorWithColor() を介して背景色を設定しようとしました: 次のように:
GContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, tableBackgroundColor());
CGContextFillRect(ctx, self.bounds);
tableBackgroundColor() は次のように黒です。
CGColorRef tableBackgroundColor()
{
static CGColorRef c = NULL;
if(c == NULL)
{
c = CreateDeviceRGBColor(0.0, 0.0, 0.0, 1.0); // black
}
return c;
}
CGColorRef CreateDeviceRGBColor(CGFloat r, CGFloat g, CGFloat b, CGFloat a)
{
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat comps[] = {r, g, b, a};
CGColorRef color = CGColorCreate(rgb, comps);
CGColorSpaceRelease(rgb);
return color;
}
ただし、この方法を使用して背景色を変更しようとするとすぐに、垂直線が消えてしまいます。ここで何が欠けていますか?
どんなアイデアでも事前に大歓迎です!