【问题标题】:iOS Swapping classes for an UIView based on condition基于条件的 UIView 的 iOS 交换类
【发布时间】:2014-01-10 21:36:10
【问题描述】:

经过研究和环顾四周 - 我迷失了一个问题。

最终我走错了路,所以请耐心等待,以防我尝试完全错误地接近它。

我有一个表格视图,其中每个单元格都可以包含一个与主题匹配的唯一视图(可以说是一个图标)。

我已将所有图标设置为自定义 UIView 类,并使用 drawRect 方法绘制它们。现在,我在 Storyboard 中有一个占位符 UIView,我用分配的标签将其取出。自定义 UIView 子类获取所有大小信息以从占位符视图中绘制自身。

我的代码如下所示:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{

UIView *eventIndicatorView = (UIView*)[cell viewWithTag:10000];

    if (conditionMatched){

    eventIndicatorView = ??? Qestion: How to assign the matching subclass of UIVIew ??? ;

    }

[eventIndicatorView setNeedsDisplay];

}

我可以通过在彼此之上添加所有可能的 UIView 来使其工作,将它们全部隐藏并根据条件取消隐藏它们。但我不太确定这真的是最好的方法。

非常感谢任何帮助。

这样的回答:“你为什么不做这个或那个”会回到这个问题:因为现在我不知道更好。所以我想提前道歉。

【问题讨论】:

    标签: ios iphone objective-c uitableview uiview


    【解决方案1】:

    你不能替换情节提要上的视图,对你来说最好的做法是创建你的自定义UIView,它与占位符视图相同的宽度/高度匹配条件,然后将其添加到占位符中子视图。

    您可以这样做:

    - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
    
        UIView *eventIndicatorView = (UIView*)[cell viewWithTag:10000];
    
        if (conditionMatched) {
    
            MyCustomView *customView = [[MyCustomView alloc] initWithFrame:CGRectMake(0, 0, eventIndicatorView.frame.size.width, eventIndicatorView.frame.size.height)];
    
            [eventIndicatorView addSubview:customView];
    
        }
    
        [eventIndicatorView setNeedsDisplay];
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2011-08-22
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      相关资源
      最近更新 更多