没有ARC的TDBadgeView nsstring崩溃

时间:2014-12-03 23:03:46

标签: ios objective-c nsstring

使用TDBadgeView,直到iOS8,现在崩溃

-[__NSArrayI sizeWithFont:]: unrecognized selector sent to instance 0x14d989f0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI sizeWithFont:]: unrecognized selector sent to instance 0x14d989f0'
*** First throw call stack:
(0x23682c1f 0x30e2dc8b 0x23688039 0x23685f57 0x23687208 0x16ca47 0x26b3f4d7 0x26567a0d 0x265633e5 0x2656326d 0x26562c51 0x26562a55 0x2655c92d 0x236493b5 0x23646a73 0x23646e7b 0x23595211 0x23595023 0x2a98e0a9 0x26ba11d1 0xf2a03 0x313adaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException

似乎反对

@property (nonatomic, retain)   NSString *badgeString;

在ios8中发生了哪些变化,因为ios5现在崩溃(项目不是ARC),所以代码工作正常?

编辑: 这就是首字母调用

TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

·H

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

@interface TDBadgeView : UIView
{
}

@property (nonatomic, readonly) NSUInteger width;
@property (nonatomic, retain)   NSString *badgeString;
@property (nonatomic, assign)   UITableViewCell *parent;
@property (nonatomic, retain)   UIColor *badgeColor;
@property (nonatomic, retain)   UIColor *badgeColorHighlighted;
@property (nonatomic, assign)   BOOL showShadow;
@property (nonatomic, assign)   CGFloat radius;

@end

@interface TDBadgedCell : UITableViewCell {

}

@property (nonatomic, retain)   NSString *badgeString;
@property (readonly, retain)    TDBadgeView *badge;
@property (nonatomic, retain)   UIColor *badgeColor;
@property (nonatomic, retain)   UIColor *badgeColorHighlighted;
@property (nonatomic, assign)   BOOL showShadow;

@end

的.m

#import "TDBadgedCell.h"

@implementation TDBadgeView

@synthesize width=__width, badgeString=__badgeString, parent=__parent, badgeColor=__badgeColor, badgeColorHighlighted=__badgeColorHighlighted, showShadow=__showShadow, radius=__radius;

- (id) initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        self.backgroundColor = [UIColor clearColor];
    }

    return self;
}

- (void) drawRect:(CGRect)rect
{
    CGFloat fontsize = 11;

    CGSize numberSize = [self.badgeString sizeWithFont:[UIFont boldSystemFontOfSize: fontsize]];

    CGRect bounds = CGRectMake(0 , 0, numberSize.width + 12 , 18);
    CGFloat radius = (__radius)?__radius:4.0;

    UIColor *colour;

    if((__parent.selectionStyle != UITableViewCellSelectionStyleNone) && (__parent.highlighted || __parent.selected))
    {
        if (__badgeColorHighlighted)
        {
            colour = __badgeColorHighlighted;
        } else {
            colour = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.000f];
        }
    } else {
        if (__badgeColor)
        {
            colour = __badgeColor;
        } else {
            colour = [UIColor colorWithRed:0.530f green:0.600f blue:0.738f alpha:1.000f];
        }
    }

    // Bounds for thet text label
    bounds.origin.x = (bounds.size.width - numberSize.width) / 2.0f + 0.5f;
    bounds.origin.y += 2;

    CALayer *__badge = [CALayer layer];
    [__badge setFrame:rect];

    CGSize imageSize = __badge.frame.size;

    // Render the image @x2 for retina people
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] == YES && [[UIScreen mainScreen] scale] == 2.00)
    {
        imageSize = CGSizeMake(__badge.frame.size.width * 2, __badge.frame.size.height * 2);
        [__badge setFrame:CGRectMake(__badge.frame.origin.x,
                                     __badge.frame.origin.y,
                                     __badge.frame.size.width*2,
                                     __badge.frame.size.height*2)];
        fontsize = (fontsize * 2);
        bounds.origin.x = ((bounds.size.width * 2) - (numberSize.width * 2)) / 2.0f + 1;
        bounds.origin.y += 3;
        bounds.size.width = bounds.size.width * 2;
        radius = radius * 2;
    }

    [__badge setBackgroundColor:[colour CGColor]];
    [__badge setCornerRadius:radius];

    UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSaveGState(context);
    [__badge renderInContext:context];
    CGContextRestoreGState(context);

    CGContextSetBlendMode(context, kCGBlendModeClear);

    [__badgeString drawInRect:bounds withFont:[UIFont boldSystemFontOfSize:fontsize] lineBreakMode:NSLineBreakByClipping];

    CGContextSetBlendMode(context, kCGBlendModeNormal);

    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [outputImage drawInRect:rect];

    if((__parent.selectionStyle != UITableViewCellSelectionStyleNone) && (__parent.highlighted || __parent.selected) && __showShadow)
    {
        [[self layer] setCornerRadius:radius];
        [[self layer] setShadowOffset:CGSizeMake(0, 1)];
        [[self layer] setShadowRadius:1.0];
        [[self layer] setShadowOpacity:0.8];
    } else {
        [[self layer] setCornerRadius:radius];
        [[self layer] setShadowOffset:CGSizeMake(0, 0)];
        [[self layer] setShadowRadius:0];
        [[self layer] setShadowOpacity:0];
    }

}

- (void) dealloc
{
    __parent = nil;
    [__badgeString release];
    [__badgeColor release];
    [__badgeColorHighlighted release];
    [super dealloc];
}

@end


@implementation TDBadgedCell

@synthesize badgeString, badge=__badge, badgeColor, badgeColorHighlighted, showShadow;

#pragma mark - Init methods

- (void)configureSelf
{
    // Initialization code
    __badge = [[TDBadgeView alloc] initWithFrame:CGRectZero];
    self.badge.parent = self;

    [self.contentView addSubview:self.badge];
    [self.badge setNeedsDisplay];
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if ((self = [super initWithCoder:decoder]))
    {
        [self configureSelf];
    }
    return self;
}

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

#pragma mark - Drawing Methods

- (void) layoutSubviews
{
    [super layoutSubviews];

    if(self.badgeString)
    {
        //force badges to hide on edit.
        if(self.editing)
            [self.badge setHidden:YES];
        else
            [self.badge setHidden:NO];


        CGSize badgeSize = [self.badgeString sizeWithFont:[UIFont boldSystemFontOfSize: 11]];
        CGRect badgeframe = CGRectMake(self.contentView.frame.size.width - (badgeSize.width + 25),
                                       (CGFloat)round((self.contentView.frame.size.height - 18) / 2),
                                       badgeSize.width + 13,
                                       18);

        if(self.showShadow)
            [self.badge setShowShadow:YES];
        else
            [self.badge setShowShadow:NO];

        [self.badge setFrame:badgeframe];
        [self.badge setBadgeString:self.badgeString];

        if ((self.textLabel.frame.origin.x + self.textLabel.frame.size.width) >= badgeframe.origin.x)
        {
            CGFloat badgeWidth = self.textLabel.frame.size.width - badgeframe.size.width - 10.0f;

            self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x, self.textLabel.frame.origin.y, badgeWidth, self.textLabel.frame.size.height);
        }

        if ((self.detailTextLabel.frame.origin.x + self.detailTextLabel.frame.size.width) >= badgeframe.origin.x)
        {
            CGFloat badgeWidth = self.detailTextLabel.frame.size.width - badgeframe.size.width - 10.0f;

            self.detailTextLabel.frame = CGRectMake(self.detailTextLabel.frame.origin.x, self.detailTextLabel.frame.origin.y, badgeWidth, self.detailTextLabel.frame.size.height);
        }

        //set badge highlighted colours or use defaults
        if(self.badgeColorHighlighted)
            self.badge.badgeColorHighlighted = self.badgeColorHighlighted;
        else
            self.badge.badgeColorHighlighted = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:1.000f];

        //set badge colours or impose defaults
        if(self.badgeColor)
            self.badge.badgeColor = self.badgeColor;
        else
            self.badge.badgeColor = [UIColor colorWithRed:0.530f green:0.600f blue:0.738f alpha:1.000f];
    }
    else
    {
        [self.badge setHidden:YES];
    }

}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    [super setHighlighted:highlighted animated:animated];
    [self.badge setNeedsDisplay];

    if(self.showShadow)
    {
        [[[self textLabel] layer] setShadowOffset:CGSizeMake(0, 1)];
        [[[self textLabel] layer] setShadowRadius:1];
        [[[self textLabel] layer] setShadowOpacity:0.8];

        [[[self detailTextLabel] layer] setShadowOffset:CGSizeMake(0, 1)];
        [[[self detailTextLabel] layer] setShadowRadius:1];
        [[[self detailTextLabel] layer] setShadowOpacity:0.8];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    [self.badge setNeedsDisplay];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];

    if (editing) 
    {
        self.badge.hidden = YES;
        [self.badge setNeedsDisplay];
        [self setNeedsDisplay];
    }
    else 
    {
        self.badge.hidden = NO;
        [self.badge setNeedsDisplay];
        [self setNeedsDisplay];
    }
}

- (void)dealloc 
{
    [__badge release];
    [badgeColor release];
    [badgeString release];
    [badgeColorHighlighted release];

    [super dealloc];
}


@end

0 个答案:

没有答案