用initWithFrame编写的类 - 我想从Storyboard实例化

时间:2013-05-30 23:14:14

标签: ios objective-c cocoa-touch instantiation initwithframe

我想使用UIExpandableTableView - https://github.com/OliverLetterer/UIExpandableTableView#readme

我遇到了一个问题,因为它只是初始化程序是initWithFrame

#pragma mark - Initialization

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if ((self = [super initWithFrame:frame style:style])) {
        self.maximumRowCountToStillUseAnimationWhileExpanding = NSIntegerMax;
        self.expandableSectionsDictionary = [NSMutableDictionary dictionary];
        self.showingSectionsDictionary = [NSMutableDictionary dictionary];
        self.downloadingSectionsDictionary = [NSMutableDictionary dictionary];
        self.animatingSectionsDictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

我一直在尝试从Storyboard初始化tableView,并意识到我看到的错误是因为永远不会调用initWithFrame并且NSMutableDictionaries没有被初始化。怎么解决这个问题?

如果不容易解决,我可以通过编程方式实例化并使用initWithFrame,但除了想要使用Storyboard之外,我还很好奇解决方案是什么。


EDIT 以下是在UIExpandableTableView中声明这些属性及其ivars的方法。这是在.h文件中:

@interface UIExpandableTableView : UITableView <UITableViewDelegate, UITableViewDataSource, NSCoding> {

@private     id __UIExpandableTableView_weak _myDelegate;     id __UIExpandableTableView_weak _myDataSource;

NSMutableDictionary *_expandableSectionsDictionary;     // will store BOOLs for each section that is expandable
NSMutableDictionary *_showingSectionsDictionary;        // will store BOOLs for the sections state (nil: not expanded, 1: expanded)
NSMutableDictionary *_downloadingSectionsDictionary;    // will store BOOLs for the sections state (nil: not downloading, YES: downloading)
NSMutableDictionary *_animatingSectionsDictionary;

NSInteger _maximumRowCountToStillUseAnimationWhileExpanding;

BOOL _onlyDisplayHeaderAndFooterViewIfTableViewIsNotEmpty;
UIView *_storedTableHeaderView;
UIView *_storedTableFooterView;

}

这是UIExpandableTableView的.m文件的顶部。 Ť

@interface UIExpandableTableView ()

@property (nonatomic, retain) NSMutableDictionary *expandableSectionsDictionary;
@property (nonatomic, retain) NSMutableDictionary *showingSectionsDictionary;
@property (nonatomic, retain) NSMutableDictionary *downloadingSectionsDictionary;
@property (nonatomic, retain) NSMutableDictionary *animatingSectionsDictionary;

@property (nonatomic, retain) UIView *storedTableHeaderView;
@property (nonatomic, retain) UIView *storedTableFooterView;

- (void)downloadDataInSection:(NSInteger)section;

- (void)_resetExpansionStates;

@end

4 个答案:

答案 0 :(得分:1)

您只需删除initWithFrame:并覆盖initWithCoder:即可。当接口构建器元素到达init时,将调用此方法以允许您设置字典。并且不必担心消除样式和框架参数,因为这两种情况都将在界面构建器中处理。

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        NSLog(@"%s",__PRETTY_FUNCTION__); // Proof of call
        self.maximumRowCountToStillUseAnimationWhileExpanding = NSIntegerMax;
        self.expandableSectionsDictionary = [NSMutableDictionary dictionary];
        self.showingSectionsDictionary = [NSMutableDictionary dictionary];
        self.downloadingSectionsDictionary = [NSMutableDictionary dictionary];
        self.animatingSectionsDictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

答案 1 :(得分:1)

您可以从UIExpandableTableView继承,并执行

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        [self configure];
    }
    return self;
}


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


-(void) configure {
    self.maximumRowCountToStillUseAnimationWhileExpanding = NSIntegerMax;
    self.expandableSectionsDictionary = [NSMutableDictionary dictionary];
    self.showingSectionsDictionary = [NSMutableDictionary dictionary];
    self.downloadingSectionsDictionary = [NSMutableDictionary dictionary];
    self.animatingSectionsDictionary = [NSMutableDictionary dictionary];
}

Nibs和Storybord实例化是通过initWithCoder:

进行的

Subcalssing允许您使用在这种情况下未修补的第三方代码。

答案 2 :(得分:0)

我会做以下事情:

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if ((self = [super initWithFrame:frame style:style])) {
        [self setup];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setup];
}

- (void)setup {
    self.maximumRowCountToStillUseAnimationWhileExpanding = NSIntegerMax;
    self.expandableSectionsDictionary = [NSMutableDictionary dictionary];
    self.showingSectionsDictionary = [NSMutableDictionary dictionary];
    self.downloadingSectionsDictionary = [NSMutableDictionary dictionary];
    self.animatingSectionsDictionary = [NSMutableDictionary dictionary];
}

答案 3 :(得分:0)

我认为这是最简单的解决方案......您不必担心视图控制器的初始化方式。

-(void)viewDidLoad
{
    [super viewDidLoad];
    self.maximumRowCountToStillUseAnimationWhileExpanding = NSIntegerMax;
    self.expandableSectionsDictionary = [NSMutableDictionary dictionary];
    self.showingSectionsDictionary = [NSMutableDictionary dictionary];
    self.downloadingSectionsDictionary = [NSMutableDictionary dictionary];
    self.animatingSectionsDictionary = [NSMutableDictionary dictionary];
}