在NSArray中创建UIVEWS的网格



我要做的是在数组中的网格中对齐视图。我创建了视图和数组。

for (int i = 0; i < [directoryContents count]; i++){

    UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,100,120)];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Show View" forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 100, 100);
    button.accessibilityIdentifier = [directoryContents objectAtIndex:i];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 100, 20)];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = [directoryContents objectAtIndex:i];

    [containerView addSubview:button];
    [containerView addSubview:label];
    [self.view addSubview:containerView];
    [_containerViewArray addObject:containerView];

}
[self layoutViews:_containerViewArray inView:self.view];

我所工作的是使它们相互对齐,我所面临的问题是我需要将它们包裹起来,而不是脱颖而出。我为他们做的是这个

    - (void)layoutViews:(NSArray *)views inView:(UIView *)contentView
{
    NSInteger overallWidth = 0;
    for ( UIView *view in views ) {
        overallWidth += view.frame.size.width;
    }
    CGSize contentSize = contentView.frame.size;
    CGFloat startOffset = (contentSize.height - overallWidth)/2.0;
    CGFloat offset = startOffset;
    for ( UIView *view in views ) {
        CGRect frame = view.frame;
        frame.origin.x = offset;
        view.frame = frame;
        offset += view.frame.size.width;
    }
    }

如果我们假设我们有一系列部分(您的视图),并且您需要每行布局3个项目,则视图的尺寸为98*98,并且水平距离为6.5px,并且垂直<8px

for (int i = 0; i< [sectionsArray count]; i++) {
    int row = (int)i/3;
    int col = i%3;
    float x = 6.5 + (104.5*col);
    float y = 8 + (106*row);
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 98, 98)];
    sectionView.backgroundColor = [UIColor clearColor];
    //button
    UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [sectionButton setTintColor:[UIColor grayColor]];
    sectionButton.frame = CGRectMake(0, 0, 98, 98);
    sectionButton.tag = i+100;
    [sectionButton addTarget:self action:@selector(showSectionDetail:) forControlEvents:UIControlEventTouchUpInside];
    [sectionView addSubview:sectionButton];
    //adding title
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 75, 98, 20)];
    titleLabel.textAlignment = UITextAlignmentCenter;
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=[UIFont fontWithName:@"Helvetica" size:14];
    titleLabel.textColor = [UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0];
    titleLabel.text = [[sectionsArray objectAtIndex:i] objectForKey:@"Title"];
    [sectionView addSubview:titleLabel];
    [titleLabel release];
    [scroll addSubview:sectionView];        
    [sectionView release];
}
int numberOfRows;
if ([sectionsArray count]/3.0f == 0) {
    numberOfRows = [sectionsArray count]/3;
}else{
    numberOfRows = [sectionsArray count]/3 +1;
}
scroll setContentSize:CGSizeMake(320, numberOfRows*106);

这是我在uicollectionView发布之前使用的,易于修改以适应您在网格中想要多少行和视图的大小。

NSUInteger buttonWidth = 100;
NSUInteger buttonHeight = 100;
NSUInteger space = 5;

float scrollContentY = (ceilf((float)imageNames.count / 3.0f) * (buttonHeight + space));
[myArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    int row = idx / 3;
    int column = idx % 3;
    UIView *view = [UIView new];
    [view setBackgroundColor:[UIColor redColor]];
    view.frame = CGRectMake((buttonWidth + space) * column + space, (buttonHeight + space) * row + space , buttonWidth, buttonHeight);
    [view setTag:(NSInteger)idx];
    [self.scrollView addSubview:view];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, scrollContentY)];
}];

请注意,如果您有一个庞大的数组来枚举,这可能不是建议,因为这没有任何可重复使用的观点。它将为阵列中的每个项目提供一个视图。

最新更新