我有一个滑出式UIView,它充当设置面板。该视图有一个容器,其中包含一个表视图控制器。表视图本身有大约6组静态单元,代表各种选项。每个单元格中有几个UIButton,根据组的不同,它们具有不同的行为。我已经创建了一个单独的自定义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];
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和&amp; viewDidLayoutSubviews但无济于事。任何建议都将不胜感激,谢谢!
答案 0 :(得分:0)
好的,我也想出了这个。因为我已经在IB中的按钮上连接了插座,所以我意识到我总是对UIButton的帧属性有一个有效的引用。我只需要在初始化/动画点将自定义视图的帧重置为当前按钮帧。所以我更改了上面的代码,添加了第3个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时以相同的方式引用它们。