ios:在自定义UITableViewCell崩溃app中使用CALayer

时间:2011-11-25 13:49:13

标签: ios uitableview 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”,这将是将在calayer上处理绘图的委托。

#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”方法时崩溃了。

1 个答案:

答案 0 :(得分: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

好的,就是这样!