iOS基于条件交换UIView的类

时间:2014-01-10 21:36:11

标签: ios iphone objective-c uitableview uiview

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

最终我走错了轨道,所以如果我试图接近它完全错误的话,请耐心等待。

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

我已将所有图标设置为自定义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];

}

我可以通过将所有可能的UIViews添加到彼此之上来实现它,将它们全部隐藏起来并根据条件取消隐藏它们。然而,我不太确定这是最好的方式。

非常感谢任何帮助。

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

1 个答案:

答案 0 :(得分:2)

您无法替换故事板上的视图,最好的操作方法是创建与占位符视图具有相同宽度/高度的条件匹配的自定义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];

}