2012-02-07 8 views
4

テーブルセルの左に円を追加しようとしていますが、これを行うには最良の方法を見つけることができません。私は追加しようとしましたiOS:iPhoneのカレンダーリストのような背景色で円を描く

CGContextRef context= UIGraphicsGetCurrentContext(); 

CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
CGContextSetAlpha(context, 0.5); 
CGContextFillEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0)); 

CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
CGContextStrokeEllipseInRect(context, CGRectMake(10.0, 10.0, 10.0, 10.0)); 

私のcellForRowAtIndexPathが無効なコンテキストエラーを取得し続けています。ここに私のcellForRowAtIndexPathがあります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"AppointmentCell"; 
    NSDictionary *appointment = [[self.appointments objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    int minutes = (int)[[NSString stringWithFormat:@"%@", [appointment objectForKey:@"start_time"]] integerValue]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
    } 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; 
    [dateFormatter setDateFormat:@"h:mma"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 
    NSDate *midnight = [NSDate dateWithTimeIntervalSince1970:0]; 
    NSDate *newDate = [midnight dateByAddingTimeInterval:minutes*60]; 

    NSString *newTime = [dateFormatter stringFromDate:newDate]; 
    dateFormatter = nil; 

    cell.textLabel.text = newTime; 
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
    cell.detailTextLabel.text = [appointment objectForKey:@"reason"]; 

    return cell; 
} 

どのように私はちょうどiPhoneのカレンダーリストビューのような色で円を追加しますか?

+2

あなたの質問には答えられませんが、そのdateFormatterをクラスのプロパティにする必要があります。セルが表示されるたびに新しいNSDateFormatterを作成します。これは必要ではなく、パフォーマンスにとって非常に悪いことです。 –

+0

、ありがとうございます – Bot

答えて

17

円を描画するための

あなたのコードを更新しましたが、あなたはそれを正しく動作させるためのUIViewのサブクラスの内部を配置する必要があります結構です。

@interface CircleView : UIView{ 
} 
@implementation CircleView{ 

- (void)drawRect:(CGRect)rect{ 
    CGContextRef context= UIGraphicsGetCurrentContext(); 

    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextSetAlpha(context, 0.5); 
    CGContextFillEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height)); 

    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
    CGContextStrokeEllipseInRect(context, CGRectMake(0,0,self.frame.size.width,self.frame.size.height)); 
} 

} 

あなたはそれはあなたが必要なものと一致するまで

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //... 
    //Your existing code 
    CGRect positionFrame = CGRectMake(10,10,10,10); 
    CircleView * circleView = [[CircleView alloc] initWithFrame:positionFrame]; 
    [cell.contentView addSubview:circleView]; 
    [circleView release]; 

    return cell; 
} 

は、位置フレームの周り再生、あなたのテーブルのセルにサークルビューを追加することができます。

+0

赤の代わりに黒い四角で表示されます。 – Bot

+2

サークルビューの背景色をクリアして、描画矩形内のself.frameをCGRectMake(0,0、self.frame.size.width、self.frame.size.height)に置き換えてみてください。 –

+0

上記のコードは、 CGContextStrokeEllipseInRect(コンテキスト、CGRectMake(1,1)、self.frame.size.width - 2、self.frame.size.height - 2));これは、 – mpemburn

2

tableView:cellForRowAtIndexPath:は、セルが作成される場所ですが、描画が行われる場所ではありません。 tableViewCellでカスタム図面を作成する場合は、UITableViewCellをサブクラス化し、drawRect:をオーバーライドして描画コードを配置する必要があります。

また、tableViewCellのimageViewのUIImageを円の画像に設定することもできます。

+0

サンプルコードを提供できますか? – Bot