标题 TableVIew(初级)
TableView的重要性:
TableView在 iOS中 开发里用到的地方99%以上,在UI中是一个非常重要的控件,主要在开发中设置属性资料往往用到。
TableView的结构:
TableView的两种样式:Plain:普通列表样式
Grouped: 这是分块风格
UITableView的构成:每一个行为Cell, 许多Cell构成了一个Section,每个Section上下分别有个Header和Footer,许多个Section组成最后一个Table,每个Table也有Header和Footer。
TableView部分方法属性实现和作用:
1.新建一个工程
2.UITableView 的初始化方法
- UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStylePlain]; //这种方法是普通的
- UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];//这种方法是分快风格 (刚开始创建不是那么清晰 这个要用到协议才能显示出来它的特色)
3.打开ViewController.h,添加协议
4.打开ViewController.m的时候发现有提示 , 朋友别看到提示就怕,没事提示的是<UITableViewDelegate, UITableViewDataSource>这个两个头文件里的协议的方法未实现,待会我们去实现它,别忘了设置代理人哦!
5.现在我们大家在- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 中建立数组 一会给每个Cell里传内容用的。
6.也是最重要的一步 创建Cell ,为了更好的节省内存, 这里用到了一个原理就是重用池
重用池的原理:假设一个界面显示5个Cell 我们创建重用池的作用就是看重用池需不需要这个CELL值,经过判断后才显示出5个Cell,Cell每消失一个立刻补充一个。
一定要注意要再循环里自动释放, 如果不再循环里自动释放程序在用户操作的时候会崩溃。
//放内容
if (!cell) {
cell = [[[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify]autorelease];//创建CELL并且看看重用池需不需要这个CELL值,把创建的那个放在里面,那么就可以节省内存和资源。
}
7一些常用的方法
//显示多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
这里可以用判断语句 这样可以每一行的属性都能设置了
//多少组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
//返回数组 存的时字符串
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E", nil];
}
//返回数组 存的时字符串 这个东西就是通讯录右侧那一行字母用处是直接返回对应的section
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"A",@"B",@"C",@"D",@"E", nil];
}
8.设置行的风格
表示UITableViewCell风格的常量有:
UITableViewCellStyleDefault
UITableViewCellStyleSubtle
UITableViewCellStyleValue1
UITableViewCellStyleValue2
可以自己修改看看效果。
cell.detailTextLabel.text = @“蒋哥天天让我降班,必须努力”