//
// RootViewController.m
// UI9_UITableView协议方法
//
// Created by YIem on 15/12/17.
// Copyright (c) 2015年 www.yiem.net YIem博客. All rights reserved.
//
import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
//////tableView
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
[tableView release];
// tableView的头部脚部区域
// 头 (只有高度起作用)
UIView *header = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
header.backgroundColor = [UIColor redColor];
tableView.tableHeaderView = header;
// 脚(x值和高度起作用)
UIView *foot = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 100, 100)];
foot.backgroundColor = [UIColor yellowColor];
tableView.tableFooterView = foot;
// tableView结构
// tableHeaderView + tableView + tableFooterView
// tableView -> section + row
// section -> header + row + footer
}
warning 必须实现的两个协议方法
// 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 设置cell (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"] autorelease];}
// 设置
// indePath 定位信息 包含section和row
cell.textLabel.text = [NSString stringWithFormat:@"s: %ld, r: %ld", indexPath.section, indexPath.row];
return cell;
}pragma mark - 其他协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
// 分区头名称 - (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
return @"头头头";
}
// 分区脚部名称 - (NSString )tableView:(UITableView )tableView titleForFooterInSection:(NSInteger)section
{
return @"脚脚脚";
}
// 自定义头部区域
- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
header.backgroundColor = [UIColor grayColor];
return [header autorelease];
}
// 自定义脚部区域 - (UIView )tableView:(UITableView )tableView viewForFooterInSection:(NSInteger)section
{
UIView *footer = [[UIView alloc]init];
footer.backgroundColor = [UIColor blackColor];
return [footer autorelease];
}
// 高度控制
// 行高
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
return 100;
}
// 头部高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
// 脚部高度 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 150;
}
// tableView右部索引栏 - (NSArray )sectionIndexTitlesForTableView:(UITableView )tableView
{
return @[@"a", @"1", @"啦"];
}
// tableView点击方法
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
NSLog(@"点击");
// 取消选中效果
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/*
pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end