UIview的子类调用自动释放导致崩溃

时间:2015-10-01 09:40:44

标签: ios objective-c iphone uitableview uiview

如果你投票不好,请解释原因。 我有一个UIView的子类用于tableview节头

@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

我在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

中使用这样的内容
 ContactSectionHeaderView *sectionHeader = [[[ContactSectionHeaderView alloc] initWithFrame:[tableView rectForHeaderInSection:section]] autorelease];

autorelease导致崩溃。如果我删除自动释放,那么它完美地工作。我如何修复崩溃?

1 个答案:

答案 0 :(得分:-1)

删除autorelease。不需要使用ARC的保留,释放方法。 ARC为你做了