2013-05-30 41 views
-1

我想使用UIExpandableTableView - https://github.com/OliverLetterer/UIExpandableTableView#readme使用initWithFrame編寫的類 - 我想從Storyboard實例化

我碰到的一個問題,因爲它是唯一的初始化是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; 
} 

我一直在試圖從初始化的故事板和的tableView實現我看到的錯誤是因爲這initWithFrame從來都不是被調用並且NSMutableDictionaries未被初始化。如何解決這個問題?

如果不容易解決,我可以通過編程實例化並使用initWithFrame,但除了想要使用Storyboard之外,我也很好奇解決方案是什麼。


編輯 這裏是如何將這些屬性和它們的ivars在UIExpandableTableView聲明。這是在.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 

回答

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]; 
} 
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; 
} 
+0

因此,這似乎是一個簡單的解決方案,但是不是一般的第三方代碼的好習慣,而不是複製它並改變它?我錯在認爲? – Ramsel

+0

@SmoothAlmonds我只是在演示如何進行更改,但你是對的,最好做一個子類。 –

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

筆尖和Storybord實例通過initWithCoder:

Subcalssing是不要讓你在這種情況下使用第三方代碼打補丁。

+0

所以我想這是一個解決方案,並嘗試過,但是,我的屬性得到了錯誤。因此,UIExpandableTableView具有在實現文件的界面中列出的屬性,如上面的編輯所示。我在子類中獲得'property ... not found on type of ... ..'對象。有沒有辦法在我的子類中訪問這些屬性? – Ramsel

+0

因此,在.h文件中也聲明瞭私有的ivars,這些都包含在編輯中。 – Ramsel

+0

重新申報 – vikingosegundo

0

我認爲這是最簡單的解決方案...您不必擔心視圖控制器如何初始化。

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