快捷方式代码(根据数量隐藏视图)
Posted
技术标签:
【中文标题】快捷方式代码(根据数量隐藏视图)【英文标题】:Shortcut the code (hide views based on count of number) 【发布时间】:2017-02-21 06:35:14 【问题描述】:下面最好的代码是什么,代码运行良好,没有问题,只是找到了更好的代码编写方式,有 11 个视图和用户数,所以我需要隐藏视图基于 11 个可用的用户假设我们有 11 个用户并且只有 2 个可用,所以它将是 11 个中的 2 个。应用程序将隐藏第一个视图和第二个视图,但不会隐藏从 3 到 11 的视图,依此类推
NSInteger countOfViews = 2;
if (countOfViews == 1)
UIView *viewtag1 = (UIView *)[self.leftScrollView viewWithTag:1000];
UIView *viewtag2 = (UIView *)[self.leftScrollView viewWithTag:2000];
UIView *viewtag3 = (UIView *)[self.leftScrollView viewWithTag:3000];
UIView *viewtag4 = (UIView *)[self.leftScrollView viewWithTag:4000];
UIView *viewtag5 = (UIView *)[self.leftScrollView viewWithTag:5000];
UIView *viewtag6 = (UIView *)[self.leftScrollView viewWithTag:6000];
UIView *viewtag7 = (UIView *)[self.leftScrollView viewWithTag:7000];
UIView *viewtag8 = (UIView *)[self.leftScrollView viewWithTag:8000];
UIView *viewtag9 = (UIView *)[self.leftScrollView viewWithTag:9000];
UIView *viewtag10 = (UIView *)[self.leftScrollView viewWithTag:10000];
UIView *viewtag11 = (UIView *)[self.leftScrollView viewWithTag:11000];
viewtag1.hidden = YES;
viewtag2.hidden = NO;
viewtag3.hidden = NO;
viewtag4.hidden = NO;
viewtag5.hidden = NO;
viewtag6.hidden = NO;
viewtag7.hidden = NO;
viewtag8.hidden = NO;
viewtag9.hidden = NO;
viewtag10.hidden = NO;
viewtag11.hidden = NO;
if (countOfViews == 2)
UIView *viewtag1 = (UIView *)[self.leftScrollView viewWithTag:1000];
UIView *viewtag2 = (UIView *)[self.leftScrollView viewWithTag:2000];
UIView *viewtag3 = (UIView *)[self.leftScrollView viewWithTag:3000];
UIView *viewtag4 = (UIView *)[self.leftScrollView viewWithTag:4000];
UIView *viewtag5 = (UIView *)[self.leftScrollView viewWithTag:5000];
UIView *viewtag6 = (UIView *)[self.leftScrollView viewWithTag:6000];
UIView *viewtag7 = (UIView *)[self.leftScrollView viewWithTag:7000];
UIView *viewtag8 = (UIView *)[self.leftScrollView viewWithTag:8000];
UIView *viewtag9 = (UIView *)[self.leftScrollView viewWithTag:9000];
UIView *viewtag10 = (UIView *)[self.leftScrollView viewWithTag:10000];
UIView *viewtag11 = (UIView *)[self.leftScrollView viewWithTag:11000];
viewtag1.hidden = YES;
viewtag2.hidden = YES;
viewtag3.hidden = NO;
viewtag4.hidden = NO;
viewtag5.hidden = NO;
viewtag6.hidden = NO;
viewtag7.hidden = NO;
viewtag8.hidden = NO;
viewtag9.hidden = NO;
viewtag10.hidden = NO;
viewtag11.hidden = NO;
/*
till if (countOfViews == 11)
*/
【问题讨论】:
您可以使用Outlet collection 来创建网点阵列,这样会更干净 【参考方案1】: NSInteger countOfViews = 2;
for (int i = 1; i <= 11; i++)
UIView *view = (UIView *)[self.leftScrollView viewWithTag:i * 1000];
view.hidden = (i <= countOfViews);
【讨论】:
干净整洁 +1 聪明。非常感谢 +1以上是关于快捷方式代码(根据数量隐藏视图)的主要内容,如果未能解决你的问题,请参考以下文章