一 UITableViewspa
//UITableView 继承于UIScrollView代理
//咱们以后看到的全部可以本身滑动的视图的控件 所有都继承于UIScrollView的orm
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];继承
//铺放数据源的代理设置 须要遵照UITableViewDataSource协议图片
tableView.dataSource = self;string
//一些辅助性的代理方法 调整tableView样式 以及每个cell的点击方法it
tableView.delegate = self;io
//设置没有分割线table
tableView.SeparatorStyle = UITableViewCellSeparatorStyleNone;class
//设置每一行的高度----一般状况下只在方法中来设立行高
tableView.rowHeight = 100;
//设置分区表头高度
tableView.sectionHeaderHeight = 100;
//设置分区表尾高度
tableView.sectionFooterHeight = 100;
#pragma mark --- 返回多少分区 默认是1个---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark --- 必须实现的 每一个分区下 返回多少行 ---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 每一个分区下 返回不一样的行数
/*
if (section == 0)
{
return 20;
}
else if(section == 1)
{
return 9;
}
return 13;
*/
return 20;
}
#pragma mark --- 返回cell的方法 必须实现的 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]autorelease];
static NSString *cellIndentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
}
// 设置主标题
cell.textLabel.text = [NSString stringWithFormat:@"第%ld分区", indexPath.section];
// 设置右边的辅助视图
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// 设置副标题
cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];
// 设置点击渲染颜色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 自定义点击渲染颜色
// 前提条件:渲染颜色不能设置成 none
/*
UIView *myView = [[UIView alloc]init];
myView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myView;
[myView release];
*/
// 设置图片
cell.imageView.image = [UIImage imageNamed:@"0.png"];
// 辅助视图设置成本身的图片
/*
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
imageView.image = [UIImage imageNamed:@"0.png"];
cell.accessoryView = imageView;
[imageView release];
*/
return cell;
}
/*
#pragma mark --- 设置每行高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
return 200;
}
else
{
return 100;
}
}
return 50;
}
*/
#pragma mark --- 点击cell的方法 ---
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"点击了 %ld分区 的 %ld行", indexPath.section, indexPath.row);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
}
/*
#pragma mark --- 返回右边的标题 ---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"A", @"B", @"C"];
}
#pragma mark --- 返回每一个分区的标题 ---
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return @"A";
}
else if (section == 1)
{
return @"B";
}
return @"C";
}
*/
/*
#pragma mark --- 分区表头高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
#pragma mark --- 自定义分区表头 ---
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myView = [[[UIView alloc]init]autorelease];
myView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"第%ld分区表头", section];
[myView addSubview:label];
[label release];
return myView;
}
#pragma mark --- 设置分区表尾高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 50;
}
#pragma mark --- 自定义分区表尾 ---
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *myView = [[[UIView alloc]init]autorelease];
myView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"第%ld分区表尾", section];
[myView addSubview:label];
[label release];
return myView;
}