gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:46:46 25 4
gpt4 key购买 nike

我正在尝试在我的表格单元格左侧添加一个圆圈,但无法找出最佳方法。我尝试添加

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 的日历 ListView 一样颜色的圆圈?

最佳答案

已更新

您用于渲染圆圈的代码没有问题,您只需将其放在 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));
}

}

然后您可以将圆形 View 添加到您的表格单元格中,

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

调整位置框架,直到它符合您的需要。

关于iphone - iOS : Make a circle with background color like the iPhone calendar list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9183392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com