2012-02-07 243 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。這不是必要的,對於性能來說非常糟糕。 – 2012-02-07 20:39:34

+0

會做,謝謝 – Bot 2012-02-07 20:50:44

回答

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 2012-02-07 21:08:12

+2

嘗試設置圓視圖的背景顏色以清除並將繪圖矩形中的self.frame替換爲CGRectMake(0,0,self.frame.size.width,self.frame.size.height) – 2012-02-07 22:46:55

+0

上面的代碼生成一個(這對我來說,無論如何):CGContextStrokeEllipseInRect(context,CGRectMake(1,1,self.frame.size.width - 2,self.frame.size.height - 2)); – mpemburn 2012-07-26 13:20:26

2

tableView:cellForRowAtIndexPath:是創建單元格的地方,但這不是繪圖發生的地方。如果你想要做自定義在tableViewCell繪圖,你需要繼承UITableViewCell,覆蓋drawRect:,並把你的繪圖代碼在那裏。

或者,您可以將您的tableViewCell的imageView的UIImage設置爲圓形圖片。

+0

你能提供示例代碼嗎? – Bot 2012-02-07 21:08:50