UITableViewCellSeparatorStyleSingleLineEtched Grouped类型使用
DataSouce数据源 负责给tableView提供数据
表视图的配置
NSIndexPath
两个属性:section(分区) row(行数)
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//1、建立TableView
UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(10, 20, 300, 450) style:UITableViewStyleGrouped];
tableview.backgroundColor = [UIColor colorWithRed:0.5 green:0 blue:0.8 alpha:0.6];
[self.view addSubview:tableview];
[tableview release];
tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableview.separatorColor = [UIColor blackColor];
tableview.rowHeight = 150;
//2、设置TableView的两个代理
//让代理人响应TableView的一些状态(点击事件/视图操作)
tableview.delegate = self;
//让代理人(ViewController)给TableView提供数据(多少行,每行有哪些内容)
tableview.dataSource = self;
}
//给分区顶部设置一个视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor greenColor];
return view;
}
//给分区底部设置一个视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
view.backgroundColor = [UIColor blueColor];
return view;
}
#pragma mark - tableView的delegate协议方法
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}
#pragma mark - tableView的dataSouce协议方法
//给每一个分区设置标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"付莉莉";
}
return @"分区“";
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"尚需",@"姜维东",@"马晓庆"];
}
//分区的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
//每一行的行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
//3、代理人实现协议中要求的方法
//设置TableView的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 20;
}
//每行的cell显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell~"];
//中间的标签视图
cell.textLabel.text = [NSString stringWithFormat:@"section:%d row:%d",indexPath.section,indexPath.row];
//左侧的图片视图
cell.imageView.image = [UIImage imageNamed:@"76.jpg"];
//右侧的附属视图
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
// cell setAccessoryView: