iOS: создайте круг с цветом фона, как в календаре iPhone - PullRequest
5 голосов
/ 08 февраля 2012

Я пытаюсь добавить кружок слева на ячейки таблицы, но не могу найти лучший способ сделать это.Я пытался добавить

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 ]

18 голосов
/ 08 февраля 2012

ОБНОВЛЕНО

Ваш код для рендеринга круга в порядке, вам просто нужно поместить его в подкласс 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;
}

Поиграйте с рамкой позиции, пока она не будет соответствовать тому, что вам нужно.

2 голосов
/ 08 февраля 2012

tableView:cellForRowAtIndexPath: - это место, где создаются ячейки, но не там, где происходит рисование. Если вы хотите сделать собственное рисование в tableViewCell, вам нужно создать подкласс UITableViewCell, переопределить drawRect: и поместить туда код для рисования.

В качестве альтернативы, вы можете установить UIImage вашего viewViewCell imageView в изображение круга.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...