您的位置:首页 > 移动开发 > IOS开发

ios 多列表格

2017-03-20 21:13 148 查看
可能是一直忙着赶项目,所以已这个为借口很久都没有写博客了,今天突然看到一篇文章谈到写博客的重要性,才刚看了开头。我就已经意识到,应该不定时的写点儿什么东西出来。虽然平时对开发有做笔记。但是都没有整理成博客发表出来,整理的东西都是针对自己的项目当中的。不过应该还是有点儿帮助吧。

在项目当中有个需要用到多列表格的东西。我以为tableView可以完成类似的功能。查了半天都没有搞定。所以最后决定自己用线条来画吧。这也是这个自定义多列表格的核心,还是挺简单的。

.h文件:

#import <UIKit/UIKit.h>

@interface UserLevelEXPTabelView : UIView

/*
使用说明:
//设置数据
NSArray *array = @[@[@"俱乐部等级",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"],@[@"游戏房间数量",@"2",@"2",@"2",@"2",@"2",@"2",@"2",@"2"],@[@"管理员数量",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"]];
//设置行高,列,行数(不包括头这一行),
[_levelTabelView setTableWithRowHeight:70 coumn:3 row:8 dataArray:array];
*/
@property (nonatomic,strong) NSArray *arrayTitle;//标题头名称

- (void) setTableWithRowHeight:(CGFloat) rowHeight coumn:(NSInteger) column row:(NSInteger) row dataArray:(NSArray *)array;
@end

.m文件:
#import "UserLevelEXPTabelView.h"
#import "BQScreenAdaptation.h" //等比例适配工具
@implementation UserLevelEXPTabelView

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupUI];
}
return self;
}
#pragma mark -- Function
- (void) setupUI{

}
- (void) setTableWithRowHeight:(CGFloat) rowHeight coumn:(NSInteger) column row:(NSInteger) row dataArray:(NSArray *)array{
//画横线
for(NSInteger i=0;i<row+2;i++){
UIView *line = [[UIView alloc]initWithFrame:BQAdaptationFrame(0, i*rowHeight, IPHONE_WIDTH-40, 1.0)];
line.backgroundColor = [UIColor lightGrayColor];
[self addSubview:line];
}
//画竖线
CGFloat margin = (IPHONE_WIDTH-40.0) / column;
CGFloat height = rowHeight * (row+1);
for(NSInteger i=0;i<column+1;i++){
UIView *line = [[UIView alloc]initWithFrame:BQAdaptationFrame(i*margin, 0, 1.0, height)];
line.backgroundColor = [UIColor lightGrayColor];
[self addSubview:line];
}
//添加label
[array enumerateObjectsUsingBlock:^(NSArray * _Nonnull obj, NSUInteger idx1, BOOL * _Nonnull stop) {
[obj enumerateObjectsUsingBlock:^(NSString * _Nonnull obj2, NSUInteger idx2, BOOL * _Nonnull stop) {
UILabel *label = [[UILabel alloc]initWithFrame:BQAdaptationFrame(idx1*margin, idx2*rowHeight, margin, rowHeight)];
label.text = obj2;
label.font = [UIFont systemFontOfSize:28*BQAdaptationWidth()];
label.textAlignment = NSTextAlignmentCenter;
[self addSubview:label];
}];
}];
}
截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  object-c 表格