/*緩存
今天須要掌握的性能優化
1.UITableView以及兩種風格和三部分性能
風格:優化
UITableViewStylePlain普通風格spa
UITableViewStyleGrouped分組風格代理
三部分: 表頭 表尾 Cellxml
2. UITableViewController對象
就是一個自帶UITableView的控制器圖片
3. UITableViewCell以及四種風格資源
風格:
* UITableViewCellStyleDefault
圖片居左,textlabel在圖片右邊,detailTextLabel默認不顯示
* UITableViewCellStyleValue1
圖片居左,textlabel在圖片的右邊,detailTextLabel居右
* UITableViewCellStyleSubtitle
圖片居左,textLabel在圖片的右邊,deetailTextlabel在textlabel的下方。
* UITableViewCellStyleValue2
左邊一個主標題textLabel,挨着右邊一個副標題detailTextLabel
經過代理給UITableView設置Cell
性能優化
經過代理添加刪除cell
*/
#pragma UITableView 風格 三部分
/*
1.UITableView 以及兩種風格和三部分
風格:
UITableViewStylePlain普通風格
UITableViewStyleGrouped分組風格
三部分:
tableHeaderView 表頭
tableFooterView 表尾
Cell
表頭 表尾根據咱們的須要而選擇是否添加
*/
#import "ContactViewController.h"
@interface ContactViewController ()<UITableViewDataSource,UITableViewDelegate>
{
UITableView *tabView1;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *tabView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 414, 736) style:UITableViewStyleGrouped];
tabView.backgroundColor = [UIColor grayColor];
tabView.tableHeaderView = [self addHeaderView];//給UITableView設置表頭
tabView.tableFooterView = [self addFooterView];//給UITableView設置表尾
tabView.delegate = self;
tabView.dataSource = self;
[self.view addSubview:tabView];
}
//建立表頭視圖
-(UIView *)addHeaderView{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
lable.text = @"表頭";
lable.backgroundColor = [UIColor greenColor];
return lable;
}
//建立表尾視圖
-(UIView *)addFooterView{
UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
lable.text = @"表尾";
lable.backgroundColor = [UIColor greenColor];
return lable;
}
#pragma UITableViewController
/*
就是一個自帶UITableView的控制器
*/
#pragma 經過代理給UITableView設置Cell 以及四種風格
* UITableViewCellStyleDefault
圖片居左,textlabel在圖片右邊,detailTextLabel默認不顯示
* UITableViewCellStyleValue1
圖片居左,textlabel在圖片的右邊,detailTextLabel居右
* UITableViewCellStyleSubtitle
圖片居左,textLabel在圖片的右邊,deetailTextlabel在textlabel的下方。
* UITableViewCellStyleValue2
左邊一個主標題textLabel,挨着右邊一個副標題detailTextLabel
UITableViewCellAccessoryNone 啥也沒有
如下是關於cell右側的標記也是Accessory
右邊出現小箭頭
UITableViewCellAccessoryDisclosureIndicator;
圈i加箭頭
UITableViewCellAccessoryDetailDisclosureButton;
對號
UITableViewCellAccessoryCheckmark;
圈i
UITableViewCellAccessoryDetailButton;
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 20;
}// 定義多少行 -----代理實現方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*
UITableView每次滑動 一定有消失的cell,系統會自動將這些消失的cell放到緩存池裏,須要新cell時,系統如今緩存池裏看是否有cell 有的就利用,沒有的就新建
前提 UITableView滑動
1.舊的cell消失,系統自動將這個cell放到緩存池裏
2.新的cell要顯示就要用到代理方法
2.1首先看看緩存池裏有沒有cell
2.2若是有就利用若是沒有就新建
2.3返回cell
*/
static NSString *cellId = @"cellID";
UITableViewCell *cell = [tabView1 dequeueReusableCellWithIdentifier:cellId];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
}
cell的三大屬性 textLabel detailTextLabel imageView
cell.textLabel.text = @"標題";
cell.detailTextLabel.text = @"副標題";
cell.imageView.image = [UIImage imageNamed:@"圖片名字"];
return cell;
}//每行cell的風格 每次出現新的cell就要觸發 -----代理方法
另外咱們的tableView還有一個分組格式
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{
return 2;
}//指定有多少分組 ----代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 20;
}//改變cell的高度 ------cell的其餘的大小不能調整,只能調整它的高度,其餘系統默認爲起始位置(0.0)寬爲屏幕的寬
UITableViewStylePlain 表頭不在tableView上通常表頭不會跟着cell移動
UITableViewStyleGrouped 表頭跟着一塊兒動 有分隔在組組之間的
須要分組cell的樣式必須使用initWithFrame: style:初始化
使用initWithFrame:初始化默認的是平鋪的樣式,不能後期更改樣式設置
UITableViewDelegate 是tableView視圖相關 的代理 機制重用機制 滾筒原理 只有固定一屏的視圖,超出重用超出視圖的cell 只是更改他的數據
UITableViewDataSource 是數據相關的代理方法
DataSource有兩個必須實現的代理方法沒實現 就會出現警告 運行就崩潰 緣由沒有實現代理方法
對於TableView的一些代理方法,這些都挺經常使用的