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



我想使用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,但除了想要使用故事板,我也很好奇一个解决方案会是什么。


编辑这些属性和它们的变量是如何在UIExpandableTableView中声明的。在.h文件中:

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

@privateid __uiexpandableview_weak_mydelegate;id __uiexpandableview_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;

}

这是uiexpandableview .m文件的顶部。T

@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

您所要做的就是删除initWithFrame:并覆盖initWithCoder:。当接口构建器元素达到init以允许您设置字典时,将调用此方法。不要担心消除style和frame参数,因为这两种情况都将在接口构建器中处理。

- (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;
}

你可以从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];
}

笔尖和故事板实例化是通过initWithCoder:完成的

子类化允许您在这种情况下使用未打补丁的第三方代码。

我会这样做:

- (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];
}

我认为这是最简单的解决方案…你不必担心视图控制器是如何初始化的

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

相关内容

  • 没有找到相关文章

最新更新