2014-09-03 5 views
0

저는 iOS를 처음 사용했기 때문에 매우 간단합니다. 중요한 점 - 하위 뷰를 추가하지 않고 도면을 사용하고 싶습니다. 그리고 공공의 방법으로해야합니다. 과 같이 그것을 시도 :UITableView 내부에 텍스트 그리기 - 텍스트가 표시되지 않습니다.

@implementation TripTableViewCell2 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)awakeFromNib 
{ 
    // Initialization code 
} 

- (void)updateWithTrip:(Trip*)trip 
{ 
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:10.0f], 
           NSForegroundColorAttributeName: UIColorFromRGB(0x28cdfb)}; 
    CGSize textSize = [trip.tripId sizeWithAttributes:attributes]; 
    CGPoint textPoint = CGPointMake(10.0f, 10.0f); 
    [trip.tripId drawAtPoint:textPoint withAttributes:attributes]; 
} 

나는 또한이보기 안에 그려 아무것도 닦아 어떤 한 줄 명령이 .. 내가 그리는하지만 확실하지 않은 상황을 설정 같은 간단한 무언가를 그리워 같은데요?

답변

1

drawAtPoint를 호출 할 때 문제는 셀의 컨텍스트가 아닌 현재 컨텍스트에 그려지는 것입니다.

대신해야 할 일은 UIView의 하위 클래스를 만들고 drawRect 메서드에서 드로잉을하는 것입니다. 여기에 내가 예상 한대로 작동하도록 클래스를 생성하고 테스트 한 :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TripCell"]; 
     cell.backgroundView = [[CustomDraw alloc] init]; // Pass your trip in here 

    return cell; 
} 
: 테이블 셀에서보기를 보여

#import "CustomDraw.h" 

@implementation CustomDraw 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 


// Only override drawRect: if you perform custom drawing. 
// An empty implementation adversely affects performance during animation. 
- (void)drawRect:(CGRect)rect { 
    NSString *trip = @"My trip"; 
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:20.0f], 
           NSForegroundColorAttributeName: [UIColor redColor]}; 
    CGSize textSize = [trip sizeWithAttributes:attributes]; 
    CGPoint textPoint = CGPointMake(10.0f, 10.0f); 
    [trip drawAtPoint:textPoint withAttributes:attributes]; 
} 


@end 

enter image description here

한 가지 방법은 배경보기로 설정하는 것입니다

0

NSString drawAtPoint:withFont:은 컨텍스트 스택을 사용하며이 메서드를 호출하는 곳에서 스택이 비어 있습니다. 전화를 걸어서

UIGraphicsPushContext(context); and UIGraphicsPopContext(); 과 같은 트릭을했습니다.

관련 문제