gpt4 book ai didi

iOS 根据条件为 UIView 交换类

转载 作者:行者123 更新时间:2023-11-29 10:49:32 26 4
gpt4 key购买 nike

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

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

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

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

我的代码如下所示:

- (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 来使其工作,将它们全部隐藏并根据条件取消隐藏它们。但我不太确定这真的是最好的方法。

非常感谢任何帮助。

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

最佳答案

好吧,你不能替换 Storyboard上的 View ,最好的做法是创建你的自定义 UIView,它与占位符 View 具有相同的宽度/高度,然后添加它作为 subview 到占位符。

以下是您可以如何做到这一点:

- (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];

}

关于iOS 根据条件为 UIView 交换类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21054844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com