实现网格/表格绘制算法

时间:2016-09-04 11:24:43

标签: ios objective-c algorithm

我试图实现用UIView s(或其他控件)生成(绘制)网格的通用方法。他们都应该有边框,我的问题是我找不到正确的算法来移动(组合)边界(所以"表"没有像现在这样的双单元格边框)并增加大小,以便视图与frame(图片中的红色)完全匹配。

以下代码生成下面的图片:

////////
    const CGFloat kOuterBorderWidth = 0.0;
        const CGFloat kInnerBorderWidth = 1.0;
        NSArray *rows;
        NSArray *columns = rows = @[@0.065, @0.29, @0.29, @0.29, @0.065];
        NSMutableArray *cells = [self drawGridWithFrame:self.frame
                                                columns:columns
                                                   rows:rows
                                       outerBorderWidth:kOuterBorderWidth
                                      innerBordersWidth:kInnerBorderWidth];
///////

和方法本身

- (NSMutableArray*)drawGridWithFrame:(CGRect)frame
                             columns:(NSArray *)columns
                                rows:(NSArray *)rows
                    outerBorderWidth:(CGFloat)outerBorderWidth
                   innerBordersWidth:(CGFloat)innerBordersWidth
{
    CGFloat x = 0, y = 0;
    NSMutableArray *cells = [NSMutableArray new];

    CGFloat height = [rows[0] floatValue] * frame.size.height;

    for (int j = 0; j < columns.count; j++) {

        CGFloat width = [columns[j] floatValue] * frame.size.width;
        CGRect cellFrame = CGRectMake(x, y, width, height);

        UIView *cellView = [[UIView alloc] initWithFrame:cellFrame];

        cellView.layer.borderWidth = innerBordersWidth;

        if (j == 0 || j == columns.count - 1) {
            cellView.layer.borderColor = [UIColor magentaColor].CGColor;
        }

        if (j == 1) {
            cellView.layer.borderColor = [UIColor orangeColor].CGColor;
        }

        if (j == 2) {
            cellView.layer.borderColor = [UIColor brownColor].CGColor;
        }

        [cells addObject:cellView];

        x += [columns[j] floatValue] * frame.size.width;

    }

    return cells;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

完成它。

注意:outerBorderWidth现在不在这里使用,如果它是&gt; 1.0pt,那么我们还应该移动视图并调整它们的大小

- (NSMutableArray*)drawGridWithFrame:(CGRect)frame
                             columns:(NSArray *)columns
                                rows:(NSArray *)rows
                    outerBorderWidth:(CGFloat)outerBorderWidth
                   innerBordersWidth:(CGFloat)innerBordersWidth
{
    CGFloat x = 0, y = 0;
    NSMutableArray *cells = [NSMutableArray new];

    CGFloat heightAdjust = (CGFloat)(rows.count - 1) / (CGFloat)rows.count;
    CGFloat widthAdjust = (CGFloat)(columns.count - 1) / (CGFloat)columns.count;

    for (int i = 0; i < rows.count; i++) {
        CGFloat height = [rows[i] floatValue] * frame.size.height + innerBordersWidth * heightAdjust;

        for (int j = 0; j < columns.count; j++) {

            CGFloat width = [columns[j] floatValue] * frame.size.width + innerBordersWidth * widthAdjust;
            CGRect cellFrame = CGRectMake(x, y, width, height);

            UIView *cellView = [[UIView alloc] initWithFrame:cellFrame];

            cellView.layer.borderWidth = innerBordersWidth;

            if (j == 0 || j == columns.count - 1) {
                cellView.layer.borderColor = [UIColor magentaColor].CGColor;
            }

            if (j == 1) {
                cellView.layer.borderColor = [UIColor orangeColor].CGColor;
            }

            if (j == 2) {
                cellView.layer.borderColor = [UIColor brownColor].CGColor;
            }

            [cells addObject:cellView];

            x += width - innerBordersWidth;
        }

        x = 0;
        y += height - innerBordersWidth;

    }

    return cells;
}

结果:

enter image description here

相关问题