快捷方式代码(根据数字隐藏视图)

时间:2017-02-21 06:35:14

标签: ios objective-c arrays views

下面最好的代码是什么,代码运行良好,没有问题只是找到更好的编写代码的方法,有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)
*/

1 个答案:

答案 0 :(得分:1)

    NSInteger countOfViews = 2;
    for (int i = 1; i <= 11; i++) {
            UIView *view = (UIView *)[self.leftScrollView viewWithTag:i * 1000];
            view.hidden = (i <= countOfViews);
    }