#import "RootViewController.h"
@interface RootViewController ()<UITableViewDataSource>
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
tableView.backgroundColor = [UIColor yellowColor];
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 100, 0);//这里的距离只有距离左右两个参数有用, 其他两个参数根据行间距决定
tableView.separatorColor = [UIColor blackColor];
//设置表头
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 80)];//把label设置成表头, label的高度就会是表头的高度, label的 X ,Y 坐标, 还有宽度都不用设置,没有影响
label.text = @"表头";
label.backgroundColor = [UIColor cyanColor];
tableView.tableHeaderView = label;
label.textAlignment = NSTextAlignmentCenter;
//[self.view addSubview:label]; 在这里的label是不能再添加到self.view上的否者就会形成两个视图, 相互覆盖
[label release];
//设置表尾(重在去除虚拟的行数), 表视图中如果没有内容, 那么中间显示的分割线就是虚拟的
/*
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
label2.backgroundColor = [UIColor blackColor];
tableView.tableFooterView = label2;//label2 的高度就是表尾的高度
*/
//既然表尾重在去除虚拟行数, 那么就不用必须要显示, 可以写成
//UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UILabel *label2 = [[UILabel alloc] init];//UILabel *label2 = [[UILabel new];new的作用与 alloc 和 init搭配的作用一样
label2.backgroundColor = [UIColor blackColor];
tableView.tableFooterView = label2;
//tableView.tableFooterView = [[UILabel alloc] init];//最终的格式
//设置数据源, 要为表视图tableView提供数据来源, 谁要提供数据,谁就要成为代理, 那么就要遵守代理的协议 dataSource
tableView.dataSource = self;//把代理给rootViewController
//成为了代理, 那么协议中的方法就可以使用
tableView.rowHeight = 100;//控制行高
[self.view addSubview:tableView];
[tableView release];
}
- (void)creatData {
NSDictionary *dic1 = @{@"name":@"阿呆", @"phone":@"123"};
NSDictionary *dic2 = @{@"name":@"阿火", @"phone":@"456"};
NSDictionary *dic3 = @{@"name":@"啊水", @"phone":@"789"};
array = @[dic1, dic2, dic3];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//这是代理UITableViewDataSource里面的方法
//这是方法里面必须要实现的, 要 :设置分区的行数
return array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//设置分区的个数, 默认值是1, 不是必须要遵守的方法
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//创建cell, 这是必须要遵守的方法, 一共两个必须遵守的方法, 这个方法是要创建cell
//每次要展示一个cell的时候都要执行此方法一次, 展示几次, 就执行几次
//这里的参数NSIndexPath 储存了cell的位置, section代表分区, row代表行数
//NSIndexPath 继承于NSObject
/*
NSLog(@"%ld, %ld", indexPath.section, indexPath.row);
UITableViewCell *tableViewCell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"辉哥!"];//tableViewCell的宽就是tableView的宽, tableViewCell的高就是tabelView的分割线的距离, 所以不用设置frame;
//tableViewCell包含contentView, 和 accessoryView(是附加视图), 其中contentView包含imageView, textLabel 和 detailTextLabel
//UITableViewCell , 单元格, 继承于UIView, 用于展示数据
//样式
//1.Default: 左侧imageView, 右侧textLabel
//2.Value1 : 左imageView , 中: textLabel , 右 : detailTextLabel
//3.Value1 : 左: textLabel , 右 : detailTextLabel
//4.Subtitle : 左imageView , 右上: textLabel , 右下 : detailTextLabel
tableViewCell.textLabel.text = [NSString stringWithFormat:@"%ld, %ld", indexPath.section, indexPath.row];
tableViewCell.imageView.image = [UIImage imageNamed:@"阿狸2.jpg"];
tableViewCell.detailTextLabel.text = @"我喜欢桃子";
tableViewCell.contentView.backgroundColor = [UIColor yellowColor];
return [tableViewCell autorelease];//这里如果用完就release这里就不能够再return了
*/
//第二种方法创建cell,
//重用机制创建Cell, 重用机制会大大减少需要创建的Cell的个数,从而节省内存
//第一步, 指定重用标志符
static NSString *str = @"笨笨";//这里用static修饰str 就是为了不会每次执行这个方法, 都会重新开辟内存空间, 导致内存浪费, 这样只会在静态去开辟一次内存空间
//第二步. 到重用池中找cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
//第三步, 判断是否能找到cell
if (cell == nil) {//如果找不到, 就创建cell
cell = [[[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:str]autorelease];//不能手动release, 刚创建完还没用不能 手动release
}
//第四步, 给cell赋值
/*
cell.imageView.image = [UIImage imageNamed:@"阿狸2.jpg"];
cell.textLabel.text = @"西瓜";
return cell;
*/
//1.通过字典
//给cell赋值
NSDictionary *dic= [array objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [dic valueForKey:@"name"];
cell.textLabel.text = [dic valueForKey:@"phone"];
return cell;
}
@end
//总结
//UITableView如何展示数据
//1.设置dataSource
//2.遵守协议<UITableViewDataSource>
//3.实现方法协议, 其中两个方法是required(分区的行数, cell的创建)
//通过重用机制创建cell
//1.重用标识符(static)修饰
//2.tableView 根据重用标识符, 去复用池中找cell
//3.判断是否找到cell, 如果没有找到cell, 就创建cell
//4.cell展示
//5.返回cell
//UITabelViewCell的代理里面的方法
//1.分区个数
//2.分区行数
//3.创建cell
//4.区头标题
//5.区尾标题
//6.分区索引