【问题标题】:Subclass of UIview Calling autorelease causes crashUIview的子类调用autorelease导致crash
【发布时间】:2015-12-29 07:32:08
【问题描述】:

如果您投反对票,请解释原因。 我有一个 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 导致了崩溃。如果我删除自动释放,那么它会完美运行。如何修复崩溃?

【问题讨论】:

  • 你的项目是 ARC 还是非 ARC ?
  • 你不需要使用autorelease,因为现在一切都是弧形的,它是自动完成的,你使用的是石器时代代码。
  • 我的项目在非 ARC @AshishThakkar
  • @iphonic,是的,这是一个石器时代的项目。
  • @tausun 将其转换为ARC,并删除非arc代码。

标签: ios objective-c iphone uitableview uiview


【解决方案1】:

删除autorelease。 ARC 不需要使用保留、释放方法。 ARC 为您服务

【讨论】:

  • 我的项目在非ARC中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 2018-02-19
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多