UITableView分为两种style:UITableViewStyleGrouped和UITableViewStylePlain。
(一)UITableViewStyleGrouped
#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
NSArray *myData;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
myTableView.dataSource = self;
myTableView.delegate = self;
//默认分隔线设置为无
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTableView];
[self initWithData];
}
- (void)initWithData
{
myData = @[
@{
@"city":@"北京",
@"cityfooter":@"我是北京",
@"district":@[@"朝阳区",@"海淀区"],
@"districtdetail":@[@"我是朝阳区",@"我是海淀区"],
@"image":@[@"",@""],
},
@{
@"city":@"上海",
@"cityfooter":@"我是上海",
@"district":@[@"徐汇区",@"闵行区",@"浦东新区"],
@"districtdetail":@[@"我是徐汇区",@"我是闵行区",@"我是浦东新区"],
@"image":@[@"",@"",@""],
},
];
} #pragma mark --------------tableviewDataSource---------------
//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return myData.count;
}
//每组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[myData[section] objectForKey:@"district"] count];
}
//组头
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [myData[section] objectForKey:@"city"];
}
//组尾
- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [myData[section] objectForKey:@"cityfooter"];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellString = @"cellString";//cell的重用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString]; UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*, cellHeight-margin*)];
cellImageView.backgroundColor = [UIColor brownColor];
cellImageView.tag = ;
[cell addSubview:cellImageView]; UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), )];
cellText.tag = ;
[cell addSubview:cellText]; UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, )];
cellDetailText.textColor = [UIColor lightGrayColor];
cellDetailText.tag = ;
[cell addSubview:cellDetailText];
//设置点选效果为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置行右边的箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//图标
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:];
cellImageView.image = [UIImage imageNamed:[myData[indexPath.section] objectForKey:@"image"][indexPath.row]]; //标题
UILabel *cellText = (UILabel *)[cell viewWithTag:];
cellText.text = [myData[indexPath.section] objectForKey:@"district"][indexPath.row];
[self getHeightWithLabel:cellText andFontSize:]; //副标题
UILabel *cellDetailText = (UILabel *)[cell viewWithTag:];
cellDetailText.text = [myData[indexPath.section] objectForKey:@"districtdetail"][indexPath.row];
cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, );
[self getHeightWithLabel:cellDetailText andFontSize:]; //分隔线
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width, )];
[lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
[cell addSubview:lineImageView]; return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这里写点击方法
NSLog(@"点击了第%li组第%li行",(long)indexPath.section,(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect;
label.attributedText = labelString;
}
(二)UITableViewStylePlain
#import "ViewController.h"
//行高
#define cellHeight 60
//边距
#define margin 10
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *myTableView;
NSArray *myData;
} @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; myTableView = [[UITableView alloc]initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
myTableView.dataSource = self;
myTableView.delegate = self;
//默认分隔线设置为无
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview:myTableView];
[self initWithData];
}
- (void)initWithData
{
//此数组格式类似于网络开发用到过的Json格式
myData = @[
@{
@"city":@"北京",
@"district":@"朝阳区",
@"districtdetail":@"我是朝阳区",
@"image":@"",
},
@{
@"city":@"北京",
@"district":@"海淀区",
@"districtdetail":@"我是海淀区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"徐汇区",
@"districtdetail":@"我是徐汇区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"闵行区",
@"districtdetail":@"我是闵行区",
@"image":@"",
},
@{
@"city":@"上海",
@"district":@"浦东新区",
@"districtdetail":@"我是浦东新区",
@"image":@"",
},
];
} #pragma mark --------------tableviewDataSource---------------
//组数(UITableViewStylePlain样式默认为1)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [myData count];
}
//设置行数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellString = @"cellString";//cell的重用
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellString]; UIImageView *cellImageView = [[UIImageView alloc]initWithFrame:CGRectMake(margin, margin, cellHeight-margin*, cellHeight-margin*)];
cellImageView.backgroundColor = [UIColor brownColor];
cellImageView.tag = ;
[cell addSubview:cellImageView]; UILabel *cellText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellImageView.frame)+margin, margin, self.view.frame.size.width-(CGRectGetMaxX(cellImageView.frame)+margin), )];
cellText.tag = ;
[cell addSubview:cellText]; UILabel *cellDetailText = [[UILabel alloc]initWithFrame:CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, )];
cellDetailText.textColor = [UIColor lightGrayColor];
cellDetailText.tag = ;
[cell addSubview:cellDetailText]; UILabel *cellIntroduceText = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-)/, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), )];
cellIntroduceText.textColor = [UIColor darkGrayColor];
cellIntroduceText.tag = ;
[cell addSubview:cellIntroduceText];
//设置点选效果为无
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置行右边的箭头
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//图标
UIImageView *cellImageView = (UIImageView *)[cell viewWithTag:];
cellImageView.image = [UIImage imageNamed:[myData[indexPath.row] objectForKey:@"image"]]; //标题
UILabel *cellText = (UILabel *)[cell viewWithTag:];
cellText.text = [myData[indexPath.row] objectForKey:@"city"];
[self getHeightWithLabel:cellText andFontSize:]; //副标题
UILabel *cellDetailText = (UILabel *)[cell viewWithTag:];
cellDetailText.text = [myData[indexPath.row] objectForKey:@"district"];
cellDetailText.frame = CGRectMake(cellText.frame.origin.x, CGRectGetMaxY(cellText.frame)+, cellText.frame.size.width, );
[self getHeightWithLabel:cellDetailText andFontSize:];
[self getWidthWithLabel:cellDetailText andFontSize:]; //介绍信息
UILabel *cellIntroduceText = (UILabel *)[cell viewWithTag:];
cellIntroduceText.text = [myData[indexPath.row] objectForKey:@"districtdetail"];
cellIntroduceText.frame = CGRectMake(CGRectGetMaxX(cellDetailText.frame)+margin, (cellHeight-)/, self.view.frame.size.width-(CGRectGetMaxX(cellDetailText.frame)+margin), ); //分隔线
UIImageView *lineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(, cellHeight-, self.view.frame.size.width, )];
[lineImageView setBackgroundColor:[UIColor colorWithRed:234.0/255.0 green:234.0/255.0 blue:234.0/255.0 alpha:1.0]];
[cell addSubview:lineImageView]; return cell;
}
#pragma mark ---------------tableviewDelegate------------------
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cellHeight;
}
//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//这里写点击方法
NSLog(@"点击了第%li行",(long)indexPath.row);
}
//UIlabel自适应高
- (void)getHeightWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(self.view.frame.size.width-label.frame.origin.x*, ) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.height = labelStringRect.size.height;
label.frame = labelRect;
label.attributedText = labelString;
}
//UIlabel自适应宽
- (void)getWidthWithLabel:(UILabel *)label andFontSize:(CGFloat)size
{
label.numberOfLines = ;
NSMutableAttributedString *labelString = [[NSMutableAttributedString alloc] initWithString:label.text];
[labelString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(, [labelString length])];
CGRect labelStringRect = [labelString boundingRectWithSize:CGSizeMake(, label.frame.size.height) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
CGRect labelRect = label.frame;
labelRect.size.width = labelStringRect.size.width;
label.frame = labelRect;
label.attributedText = labelString;
}