2013-06-05 27 views
1

我有一個幻燈片UIView,它充當設置面板。視圖中有一個帶有表格視圖控制器的容器。表格視圖本身有大約6組代表各種選項的靜態單元組。每個單元格中都有幾個UIButton,並且取決於組,它們具有不同的行爲。我創建了一個單獨的自定義UIView子類,它處理每個組中按鈕的繪製。當第一次加載此面板時,我將爲每個組的按鈕創建一個字典詞典(針對每個組的自定義視圖的字典詞典(具有匹配的鍵路徑)&)。在整個UITableView中添加幾個自定義子視圖(設置面板)

問題是,自定義視圖只能在面板的頂部顯示。當我向下滾動到面板的底部時,按鈕視圖不排隊。對於我的生活,我似乎無法弄清楚這一點。下面是面板的圖片:

Top of panel (working as expected) Bottom of panel (bad things are happening)

這裏是我使用的初始化設置&佈局自定義子視圖代碼當面板負載(在設置面板表視圖控制器類):

- (void)initializeSettings 
    { 
     self.buttonStates = nil; // A dictionary of dictionaries with all button states within the panel 
     self.buttonViews = nil; // A dictionary of dictionaries with all button views (same keyPaths as self.buttonStates). 
     NSMutableDictionary *buttonStates = [[NSMutableDictionary alloc] init]; 
     NSMutableDictionary *buttonViews = [[NSMutableDictionary alloc] init]; 
     NSArray *btnGroupNames = [self btnGroupNames]; // Array of button group names. 
     NSArray *btnArrays = [self btnArrays]; // An array of arrays with all buttons. 
     for(int i = 0; i < [btnArrays count]; i++){ 
      NSArray *outerArray = [btnArrays objectAtIndex: i]; 
      NSString *outerKey = [btnGroupNames objectAtIndex: i]; 
      NSMutableDictionary *innerDicV = [[NSMutableDictionary alloc] init]; 
      NSMutableDictionary *innerDicS = [[NSMutableDictionary alloc] init]; 
      for(UIButton *btn in outerArray){ 
       NSString *innerKey = [btn.titleLabel.text stringByReplacingOccurrencesOfString:@" " withString:@""]; 
       ButtonSelectionView *bsv = [[ButtonSelectionView alloc] initWithFrame: btn.frame]; 
       bsv.alpha = 0.0; 
       bsv.opaque = NO; 
       [[btn superview] addSubview: bsv]; 
       [[btn superview] bringSubviewToFront: btn]; 
       [innerDicV addEntriesFromDictionary: @{ innerKey : bsv }]; 
       [innerDicS addEntriesFromDictionary: @{ innerKey : NOT_SELECTED }]; 
      } 
      [buttonViews setValue: innerDicV forKey: outerKey]; 
      [buttonStates setValue: innerDicS forKey: outerKey]; 
     } 
     self.buttonViews = buttonViews; 
     self.buttonStates = buttonStates; 
     self.previousButtonStates = nil; 
     self.settingsLoaded = YES; 
    } 

附註:這些字典是懶洋洋地實例化的。此外,我試圖從viewDidLoad,viewDidAppear,& viewDidLayoutSubviews調用此函數,但無濟於事。任何建議將不勝感激,並感謝你!

+0

好的,也許只是簡單的問題:UIButtons在什麼時候知道它們的框架在UITableViewCell中?當我將它們放置在IB中時,我可以如何獲得所有UIButtons的幀(甚至是可見視圖之外的幀)?通過出口集合枚舉似乎給了我如上圖所示的相同結果。 –

回答

0

好的,我也計算了一下。因爲我已經在IB的按鈕上連接了網點,所以我意識到我總是擁有對UIButton框架屬性的主動引用。我只需要將自定義視圖的框架重置爲初始化/動畫點的當前按鈕框架。所以我改變了上面的代碼來添加第三個NSDictionary,它通過與其他兩個字典相同的keypath來引用每個按鈕。然後,當我需要自定義的UIView更改/出現,我在字典中的幀值引用的按鈕:

- (void)initializeSettings 
    { 
     self.buttonStates = nil; // A dictionary of dictionaries with all button states within the panel 
     self.buttonViews = nil; // A dictionary of dictionaries with all button views (same keyPaths as self.buttonStates). 
     NSMutableDictionary *buttonStates = [[NSMutableDictionary alloc] init]; 
     NSMutableDictionary *buttonViews = [[NSMutableDictionary alloc] init]; 
     NSMutableDictionary *buttons = [[NSMutableDictionary alloc] init]; 
     NSArray *btnGroupNames = [self btnGroupNames]; 
     NSArray *btnArrays = [self btnArrays]; 
     for(int i = 0; i < [btnArrays count]; i++){ 
      NSArray *outerArray = [btnArrays objectAtIndex: i]; 
      NSString *outerKey = [btnGroupNames objectAtIndex: i]; 
      NSMutableDictionary *innerDicV = [[NSMutableDictionary alloc] init]; 
      NSMutableDictionary *innerDicS = [[NSMutableDictionary alloc] init]; 
      NSMutableDictionary *innerDicB = [[NSMutableDictionary alloc] init]; 
      for(UIButton *btn in outerArray){ 
       NSString *innerKey = [btn.titleLabel.text stringByReplacingOccurrencesOfString:@" " withString:@""]; 
       ButtonSelectionView *bsv = [[ButtonSelectionView alloc] initWithFrame: btn.frame]; 
       bsv.alpha = 0.0; 
       bsv.opaque = NO; 
       [[btn superview] addSubview: bsv]; 
       [[btn superview] bringSubviewToFront: btn]; 
       [innerDicV addEntriesFromDictionary: @{ innerKey : bsv }]; 
       [innerDicS addEntriesFromDictionary: @{ innerKey : NOT_SELECTED }]; 
       [innerDicB addEntriesFromDictionary: @{ innerKey : btn }]; 
      } 
      [buttonViews setValue: innerDicV forKey: outerKey]; 
      [buttonStates setValue: innerDicS forKey: outerKey]; 
      [buttons setValue: innerDicB forKey: outerKey]; 
     } 
     self.buttonViews = buttonViews; 
     self.buttonStates = buttonStates; 
     self.buttons = buttons; 
     self.previousButtonStates = nil; 
     self.settingsLoaded = YES; 
    } 

然後,當用戶點擊一個按鈕,在我的「更新UI」的方法,我加入這行代碼來重新定位自定義按​​鈕選擇視圖:

bsv.frame = ((UIButton *)[self.buttons valueForKeyPath: keyPath]).frame; 

希望這將幫助別人類似的問題。我想你可以通過在viewDidLayoutSubviews中初始化/存儲按鈕對象&自定義視圖對象並在更新UI時以相同的方式引用它們來以編程方式執行相同的操作。

相關問題