【问题标题】:How to Set the View Using for Loop?如何使用 for 循环设置视图?
【发布时间】:2016-02-10 10:05:00
【问题描述】:

我必须这样设计屏幕。基本类别包含 4 项,然后厨房类别包含 8 项,然后实用类别包含 11 项。如何实现可重用性?

这是我尝试过的我的代码:

- (void)buildAppliancesView
{
    for (int i=0; i<5; i++)
    {
        [self addAppliancesCategoriesLabel:i];
        [self addApplianceCategoryView:i];
        [self addIndividualAppliances:i];
    }
}

- (void)addAppliancesCategoriesLabel:(int)y
{
    label = [[UILabel alloc] initWithFrame:CGRectMake(0,(_appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height)+y*150+10, 0, 0)];
    NSLog(@"y=>%f",label.frame.origin.y);

    label.text =[NSString stringWithFormat:@"%@",[APPLIANCESCATEGORIESLABEL_ARRAY objectAtIndex:y]];
    [label sizeToFit];
    [_scrollView addSubview:label];
}

- (void)addApplianceCategoryView:(int)y
{
    applianceCategoryView = [[UIView alloc]initWithFrame:CGRectMake(5, label.frame.origin.y+label.frame.size.height+5, self.view.frame.size.width-10, 70)];
    [applianceCategoryView setBackgroundColor:[UIColor grayColor]];
    [_scrollView addSubview:applianceCategoryView];
}

- (void)addIndividualAppliances:(int)y
{
    for (int i=0; i<4; i++)
    {
        UIView *applianceView = [[UIView alloc]initWithFrame:CGRectMake(5+((self.view.frame.size.width/4)+5)*y, _appliancesHeaderLabel.frame.origin.y+_appliancesHeaderLabel.frame.size.height+label.frame.size.height+10, 50, 50)];
        NSLog(@"xVAlue==>>%f",applianceView.frame.origin.x);
        [applianceView setBackgroundColor:[UIColor redColor]];

        UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changecolor)];
        [applianceView addGestureRecognizer:singleTap];

        [applianceCategoryView addSubview:applianceView];
    }    
}

【问题讨论】:

  • 在评论任何需要知道该屏幕工作的内容之前,如果是,所有项目是否都可以滚动,然后朝哪个方向滚动?整个项目将滚动或仅特定?意味着如果用户滚动厨房项目,只有那些项目会滚动?
  • @VijayPalwe 没有。所有项目都必须从上到下滚动。我的问题是,如何运行 forloop 4 次、8 次和 11 次。还有如何为 $ 项目和厨房视图增加 8 个项目的基本尺寸。
  • 好的,使用滚动视图,全局获取x和y,不要创建单独的uiview,创建一个函数,在其中编写代码来创建uilable -(void)createLableForPosition:(int)xpos andYposition:(int)yPosition和另一个函数在其中写入一个 for 循环 -(void)createItems:(int)numberOfItem { for() }
  • 所以现在您可以将它用于每个类别及其项目,并使用总高度作为该滚动视图的内容大小
  • 你为什么要复制UICollectionView?只需创建一个数据模型,其中EssentialsKitchen 是以项目为行的部分。然后使用UICollectionViewController 构建您的场景。

标签: ios objective-c view


【解决方案1】:

检查这个 - 用于在循环中创建视图的示例代码。

int xPosition = 0;
int yPosition = 0;//set your y origin
int height = 150; // sample value
int width = (NumberOfItems.count)/4;
for (int i = 0; i < NumberOfItems.count; i++) {  
   UIView * view = [[UIView alloc] initFrame:CGRectMake(xPosition, yPosition, width, height)];
   [self.scrollView addSubview:view];
   xPosition += width;
   if(xPosition == 3*width){
      xPosition=0;
      yPosition = yPosition + height; // set as per your requirement
   }
} 

使用 UIScrollview 会更好。

这是您的场景的示例代码。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//    UITapGestureRecognizer *singleTap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapDetected:)];
//    singleTap.numberOfTapsRequired=1;


    UIScrollView *ScrollVc = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    ScrollVc.delegate = self;
    ScrollVc.showsHorizontalScrollIndicator = NO;
    [ScrollVc setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:ScrollVc];
    ScrollVc.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//    [ScrollVc addGestureRecognizer:singleTap];


    NSArray *essentialsArr = @[@"1",@"2",@"3"];
    NSArray *KitchenArr = @[@"1",@"2",@"3",@"4",@"5",@"6"];
    NSArray *abcArr = @[@"1",@"2",@"3",@"4"];
    NSArray *arr = [NSArray arrayWithObjects:essentialsArr,KitchenArr,abcArr, nil];

    int yPosition = 0;//set your y origin
    int yPositionOfMain = 0;//set your y origin

    for (int k = 0; k < arr.count; k++) {

        int xPosition = 0;
        int height = 100; // sample value
        int width = ([[UIScreen mainScreen] bounds].size.width)/4;

        UIView *mainview = [[UIView alloc] init];
        mainview.backgroundColor = [UIColor yellowColor];
        [ScrollVc addSubview:mainview];

        for (int i = 0; i < [[arr objectAtIndex:k] count]; i++) {
            if (i==0) {
                xPosition=0;
                yPosition= 0;
            }
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(xPosition + 10, yPosition, width-20, height)];
            view.backgroundColor = [UIColor redColor];
            [mainview addSubview:view];
            xPosition += width;
            if(xPosition == 4*width && i!=[[arr objectAtIndex:k] count]-1){
                xPosition=0;
                yPosition = yPosition + height + 10; // set as per your requirement
            }
        }
        mainview.frame = CGRectMake(0, yPositionOfMain, [[UIScreen mainScreen] bounds].size.width, yPosition + height + 10);
        yPositionOfMain = yPositionOfMain + mainview.frame.size.height + 20;
    }

    if ([UIScreen mainScreen].bounds.size.height==736)// iPhone 6 Plus         66 Navigation bar
    {
        [ScrollVc setContentSize:CGSizeMake(320,950)];
    }
    else if ([UIScreen mainScreen].bounds.size.height==667)// iphone 6       44 Navigation bar
    {
        [ScrollVc setContentSize:CGSizeMake(320,900)];
    }
    else if ([UIScreen mainScreen].bounds.size.height==568)//  iphone 5, 5C, 5S   44 Navigation bar
    {
        [ScrollVc setContentSize:CGSizeMake(320,800)];
    }
    else                                                    //  iphone 4, 4S   44 Navigation ba
    {
        [ScrollVc setContentSize:CGSizeMake(320,750)];
    }
}

请根据您的要求更改必要的值。

希望这会有所帮助。

【讨论】:

  • 我的问题是,如何运行 forloop 4 次、8 次和 11 次。以及如何完美地增加 $ 项目的基本尺寸和 8 项目的厨房视图。
  • @sathish 我必须在该数量的项目中给出什么。因为它会因不同的类别而变化......
  • NumnerOfItems 是 Essentials/kitchen 中的物品数组,或者您可以直接提及您想要在特定 Essentials/kitchen 中查看多少次的数字。
  • @sathish 没有得到你的代码....请解释清楚。如果厨房有 8 个项目,如何增加该厨房视图的高度并在第二行添加第 5 个项目... .
  • 谢谢。但是我如何为标签名称设置 Yvalue。如果我这样写它不起作用,UILabel *label = [[UILabel alloc] initWithFrame: CGRectMake(self.view.frame.size.width/2,mainView.frame.origin.y+mainView.frame.size.height+2,0,0)];
猜你喜欢
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2021-08-18
  • 1970-01-01
相关资源
最近更新 更多