简体   繁体   中英

Subclass of UIview Calling autorelease causes crash

Please explain why if you are down voting. I have a subclass of UIView for tableview section header

@interface ContactSectionHeaderView()

@property (nonatomic, retain) IBOutlet UILabel *headerTitle;

@end

@implementation ContactSectionHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];

    if(self){
        id rootView = [[self class] viewSizedForDevice];
        [rootView setFrame:frame];
        self = rootView;
    }

    return self;
}


+ (id) viewSizedForDevice {
    UINib* nib = [self nib];
    NSArray * nibObjects = [nib instantiateWithOwner:self options:nil];
    NSAssert2(([nibObjects count] > 0) &&
              [[nibObjects objectAtIndex:0] isKindOfClass:[self class]],
              @"Nib '%@' does not appear to contain a value %@",
              [self nibName], NSStringFromClass([self class]));
    id rootView = nibObjects[0];
    return rootView;
}

+(UINib*)nib {
    NSBundle * classBundle = [NSBundle bundleForClass:[self class]];
    UINib * nib = [UINib nibWithNibName:[self nibName] bundle:classBundle];
    return nib;
}

+ (NSString *)nibName {
    return [self viewIdentifier];
}

+ (NSString *)viewIdentifier {
    static NSString* _viewIdentifier = @"ContactSectionHeaderView";
    _viewIdentifier = NSStringFromClass([self class]);
    return _viewIdentifier;
}

-(void)setTitle:(NSString *)title {
    self.headerTitle.text = [NSString stringWithFormat:@"%@",title];    
}
- (void)dealloc {
    self.headerTitle = nil;
    [super dealloc];
}

@end

I am using like this in - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

 ContactSectionHeaderView *sectionHeader = [[[ContactSectionHeaderView alloc] initWithFrame:[tableView rectForHeaderInSection:section]] autorelease];

This autorelease is causing the crash. If I remove the autorelease, then it works perfectly. How do I fix the crash?

Remove autorelease . There is no need to use retain, release methods with ARC. ARC does it for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM