春节过了,没时间更新了,估计要好一段时间不能写了。今天我给大家带来简单的UITabeleView用法,估计刚开始有点难懂,多练习就行了。
#import "ViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>//数据源代理协议方法
和代理协议方法
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
[self
createTabView];
}
-(void)createTabView
{
UITableView *tableView = [[UITableView
alloc]initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
tableView.dataSource =
self;
tableView.delegate =
self;
tableView.rowHeight =
60;
tableView.sectionHeaderHeight =
50;
tableView.sectionFooterHeight =
40;
//设置头表视图
UIView *headerView = [[UIView
alloc]init];
headerView.frame =
CGRectMake(50,
50, 50,
50);
headerView.backgroundColor = [UIColor
greenColor];
tableView.tableFooterView = headerView;
//设置尾表视图
UIView *footerView = [[UIView
alloc]init];
footerView.frame =
CGRectMake(50,
50, 50,
0);
footerView.backgroundColor = [UIColor
greenColor];
tableView.tableFooterView = footerView;
[self.view
addSubview:tableView];
}
//设置组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return
10;
}
//设置行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return
5;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
static
int count = 0;
count++;
NSString *cellID =
@"cellID";
//从tableview的复用队列取用cellid标识的可复用的cell
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellID];
if (cell ==
nil) {
static
int count1 = 0;
count1++;
cell = [[UITableViewCell
alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellID];
//Set the selected cell view
UIView *selView = [[UIView
alloc]init];
selView.backgroundColor = [UIColor
cyanColor];
cell.selectedBackgroundView = selView;
}
if (indexPath.row %2) {
cell.backgroundColor = [UIColor
orangeColor];
}
else
{
cell.backgroundColor = [UIColor
purpleColor];
}
//当前组合行
在tableview上要显示的数据
NSString *string = [NSString
stringWithFormat:@"%ld组%ld行",indexPath.section,indexPath.row];
//Set the display content on the cell
cell.textLabel.text =string;
return cell;
}
//Sets the head title specified group
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"%ld组头部",section];
}
//返回指定分组的尾部标题
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [NSString
stringWithFormat:@"%ld组尾部",section];
}
//返回指定位置的行高,默认行高为44
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath
{
if (indexPath.row %2) {
return
44;
}
else
{
return
66;
}
}
//返回每个分组的头部高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
return
40;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section
{
return
20;
}
//返回指定分组的头部视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label = [[UILabel
alloc]init];
label.text = [NSString
stringWithFormat:@"第%ld组头部",section];
label.textColor = [UIColor
redColor];
label.font = [UIFont
systemFontOfSize:22];
return label;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UILabel *label = [[UILabel
alloc]init];
label.text = [NSString
stringWithFormat:@"第%ld组尾部",section];
label.textColor = [UIColor
redColor];
label.backgroundColor = [UIColor
grayColor];
label.font = [UIFont
systemFontOfSize:22];
return label;
}
@end