2011-11-25 76 views
0

我有一些代碼,我在以前的項目中使用,並在那裏工作正常。現在我使用這些舊代碼作爲解決當前項目中相同問題的指導原則。問題:現在正常工作的方法現在會崩潰我的應用程序。也許你們中的一個人看到我在這裏做錯了什麼?ios:在自定義UITableViewCell崩潰應用程序中繪製CALayer

在我當前的項目中,我有一個自定義tableviewcell,它包含一個名爲「VerlaufView」的自定義UIView。我在Interface Builder中連接了所有東西,並檢查了它的工作正常。因此,這裏是我的自定義的UIView:

接口定義:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface VerlaufView : UIView { 
    CALayer* mainLayer; 
} 

@property (strong,nonatomic) CALayer* mainLayer; 

-(void) initialisieren; 

@end 

這是落實。請注意,該實現包含內部類「VerlaufDelegate」,這將成爲處理圖層上的圖形的代理。

#import "VerlaufView.h" 


static bool debug = YES; 

#pragma mark VerlaufDelegate 
@interface VerlaufDelegate : NSObject { 
    UIView* layersParent; 
} 

@property (nonatomic,strong) UIView* layersParent; 

-(id)initWithView:(UIView*)view; 

@end 

@implementation VerlaufDelegate 
@synthesize layersParent; 


-(id)initWithView:(UIView*)view { 
    if(debug) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 
    self = [super init]; 
    if(self) 
     self.layersParent = view; 
    if(debug) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__); 
    return self; 
} 


- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    if(debug) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 
    if(debug) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__); 
} 

@end 




@implementation VerlaufView 

@synthesize mainLayer; 

-(void) initialisieren 
{ 
    if(debug) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 
    VerlaufDelegate* layerDelegate = [[VerlaufDelegate alloc] initWithView:self]; 
    float width = 200; //320; 
    float height = 100; //259; 

    //Hauptlayer 
    mainLayer = [[CALayer alloc] init]; 
    mainLayer.delegate = layerDelegate; 

    mainLayer.bounds = CGRectMake(0.f, 0.f, width, height); 
    /*mainLayer.backgroundColor = [[UIColor whiteColor] CGColor]; 
    mainLayer.cornerRadius = 10.f; 
    CGFloat components[4] = { 0.0, 0.0, 0.0, 1.0 }; 


    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); 
    CGColorRef black = CGColorCreate(colorspace, components); 
    mainLayer.borderColor = black; 
    mainLayer.borderWidth = 1.f; 
    mainLayer.shadowRadius = 5.f; 
    mainLayer.shadowColor = black; 

    CGColorRelease(black); 
    CGColorSpaceRelease(colorspace); 

    mainLayer.shadowOffset = CGSizeMake(0.f, 5.f), 
    mainLayer.shadowOpacity = 0.75f; 
    */ 

    [self.layer insertSublayer:mainLayer above:self.layer]; 
    [mainLayer setNeedsDisplay]; 
    if(debug) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__); 
} 
@end 

所以我的代碼在處理「initialisieren」方法時崩潰。

回答

0

好的,我解決了這個問題。現在它可以工作。引用計數可能存在問題。因此,我將我的委託的接口聲明放入頭文件中。下面的代碼解決了這個問題:

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@class VerlaufDelegate; 

@interface VerlaufCell : UITableViewCell { 
    VerlaufDelegate* layerDelegate; 
} 

@property (strong,nonatomic) VerlaufDelegate* layerDelegate; 

@end 


#pragma mark VerlaufDelegate 
@interface VerlaufDelegate : NSObject { 
    UIView* layersParent; 
} 

@property (nonatomic,strong) UIView* layersParent; 

-(id)initWithView:(UIView*)view; 

@end 

和實現:

#import "VerlaufCell.h" 

static bool debug = YES; 

@implementation VerlaufDelegate 
@synthesize layersParent; 


-(id)initWithView:(UIView*)view { 
    if(debug) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 
    self = [super init]; 
    if(self) 
     self.layersParent = view; 
    if(debug) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__); 
    return self; 
} 


- (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx { 
    if(debug) NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__); 

    NSString* text = @"VerlaufCellDelegate"; 
    float h = 20.f; 

    CGContextSelectFont(ctx, "Helvetica-Bold", h, kCGEncodingMacRoman); 
    //CGContextSetCharacterSpacing(ctx, 10); 
    CGContextSetTextDrawingMode(ctx, kCGTextFillStroke); 
    CGContextSetRGBFillColor(ctx, 1, 1, 1, 1); 
    CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1); 

    CGAffineTransform transform; 
    transform.a = 1; 
    transform.b = 0; 
    transform.c = 0; 
    transform.d = -1; 
    transform.tx = 0; 
    transform.ty = 0; 
    CGContextSetTextMatrix(ctx, transform); 

    CGContextShowTextAtPoint(ctx, 50, 25, [text cStringUsingEncoding:NSASCIIStringEncoding], [text length]);  


    if(debug) NSLog(@">>> Leaving %s <<<", __PRETTY_FUNCTION__); 
} 

@end 


@implementation VerlaufCell 

@synthesize layerDelegate; 

- (id) initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if(self) { 
     self.layerDelegate = [[VerlaufDelegate alloc] initWithView:self.contentView]; 

     CALayer *customDrawn = [CALayer layer]; 
     customDrawn.delegate = self.layerDelegate; 
     customDrawn.backgroundColor = [UIColor greenColor].CGColor; 
     customDrawn.frame = CGRectMake(10, 10, 300, 230); 
     customDrawn.shadowOffset = CGSizeMake(0, 3); 
     customDrawn.shadowRadius = 5.0; 
     customDrawn.shadowColor = [UIColor blackColor].CGColor; 
     customDrawn.shadowOpacity = 0.8; 
     customDrawn.cornerRadius = 10.0; 
     customDrawn.borderColor = [UIColor blackColor].CGColor; 
     customDrawn.borderWidth = 2.0; 
     customDrawn.masksToBounds = YES; 
     [[self layer] addSublayer:customDrawn]; 
     [customDrawn setNeedsDisplay]; 
    } 
    return self; 
} 

@end 

好了,這就是它!

相關問題