1

我有一个滑出式 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 和 viewDidLayoutSubviews 调用此函数,但无济于事。任何建议将不胜感激,谢谢!

4

1 回答 1

0

好的,我也想出了这个。因为我已经在 IB 中为我的按钮连接了插座,所以我意识到我总是有一个对 UIButton 的 frame 属性的活动引用。我只需要在初始化/动画时将自定义视图的框架重置为当前按钮框架。因此,我更改了上面的代码以添加第三个 NSDictionary,该 NSDictionary 通过与其他两个字典相同的键路径引用每个按钮。然后,当我需要自定义 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 时以相同的方式引用它们来以编程方式执行相同的操作。

于 2013-06-06T21:30:16.687 回答