//
// ViewController.m
// tableview
//
// Created by 方超 on 16/6/14.
// Copyright © 2016年方超. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView * tableview;
@property(nonatomic,strong)NSArray * array;
@property(nonatomic,assign)float cellheight;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableview=[[UITableViewalloc]initWithFrame:self.view.bounds];
self.tableview.delegate=self;
self.tableview.dataSource=self;
[self.viewaddSubview:self.tableview];
self.array=@[@"11122",@"safaf",@"事实上事实上",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽肤水",@"啊啊",@"是的撒飞洒发舒服舒服",@"爽",@"121",@"121"];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
returnself.array.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
returnself.cellheight;
}
- (CGSize)getSizeByString:(NSString*)string AndFontSize:(CGFloat)font
{
CGSize size = [stringboundingRectWithSize:CGSizeMake(999,25) options:NSStringDrawingTruncatesLastVisibleLine |NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeadingattributes:@{NSFontAttributeName:[UIFontsystemFontOfSize:font]}context:nil].size;
size.width +=5;
return size;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
staticNSString * strcell=@"cellstr";
UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:strcell];
if (!cell){
cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:strcell];
}
int width =0;
int height =0;
int number =0;
int han =0;
for (int i =0; i < self.array.count; i++){
UILabel *button = [[UILabelalloc]initWithFrame:CGRectMake(0,15, 50,20)];
button.tag =300 + i;
button.textAlignment=NSTextAlignmentCenter;
CGSize titleSize = [selfgetSizeByString:self.array[i]AndFontSize:13];
han = han +titleSize.width;
if (han >= [[UIScreenmainScreen]bounds].size.width-75) {
han = 0;
han = han + titleSize.width;
height++;
width = 0;
width = width+titleSize.width;
number = 0;
button.frame =CGRectMake(11,30 +25*height, titleSize.width,20);
self.cellheight=button.frame.origin.y;
}else{
button.frame =CGRectMake(width+11+(number*10),30 +25*height, titleSize.width,20);
width = width+titleSize.width;
self.cellheight=button.frame.origin.y;
}
number++;
button.enabled=NO;
button.font = [UIFontsystemFontOfSize:11];
button.layer.masksToBounds =YES;
button.layer.cornerRadius =5;
button.textColor=[UIColorwhiteColor];
button.text=[self.arrayobjectAtIndex:i];
[button.layersetBorderWidth:1];
//设置按钮的边界颜色
CGColorSpaceRef colorSpaceRef =CGColorSpaceCreateDeviceRGB();
CGColorRef color =CGColorCreate(colorSpaceRef, (CGFloat[]){0.5,0.5,0.5,0.5});
[button.layersetBorderColor:color];
[cell addSubview:button];
}
return cell;
}
@end