简体   繁体   中英

iOS: Make a circle with background color like the iPhone calendar list

I'm trying to add a circle to the left on my table cells however can't figure out the best way to do it. I tried adding

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));

to my cellForRowAtIndexPath but kept getting invalid context errors. Here is my 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;
}

How would I add the circle with a color just like the iPhone's Calendar list view?

UPDATED

Your code for rendering the circle is fine you will just have to place that inside of a UIView sub class for it to work properly.

@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));
}

}

You can then add the circle view to your table cell,

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

Play around with the position frame until it matches what you need.

tableView:cellForRowAtIndexPath: is where cells are created, but that isn't where drawing happens. If you want to do custom drawing in a tableViewCell, you'll need to subclass UITableViewCell , override drawRect: , and put your drawing code in there.

Alternatively, you could set your tableViewCell's imageView's UIImage to a picture of a circle.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM