// Created By 郭仔 2015年04月22日22:12:47
// ==================================
時間都去哪了!!!!!!!
// ==================================
表視圖 UITableView,iOS中最重要的視圖,随處可⻅見。 表視圖通常⽤用來管理⼀一組具有相同資料結構的資料。
UITableView繼承⾃自UIScrollView,是以可以滾動
表視圖的每⼀一條資料都是顯⽰示在UITableViewCell對象中
表視圖可以分區顯⽰示資料,每個分區稱為⼀一個section,每⼀一⾏行稱為 row,編号都是從0開始。
DataSource資料源:
我們需要給tableView指定⼀一個資料源,它負責給tableView提供資料 需要實作協定中兩個必須實作的⽅方法;
// ============
UITableView中每⼀一個單元格,被稱為⼀一個cell
(UITableViewCell)。 系統預置了4種(枚舉)樣式的cell。 不同樣式的cell包含的控件有細微差别。
// ============
UITableView的重用機制:
UITableView靠mutableSet來實作重⽤用功能
出螢幕的cell會被添加到mutableSet中,進⼊入螢幕的cell,先從set中 擷取,如果擷取不到,才建立⼀一個cell。在cell顯⽰示之前,給cell賦上
相應的内容。
cell的reuseIdentifier是重⽤用的關鍵;
// =============
tableView預設是⼀一個分區,可以設定多個分區 tableView的plain、group樣式決定分區的樣式不同
每個分區可以設定區頭區尾;
// =============
UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, 320, 400) style:UITableViewStylePlain];
// tableView.rowHeight = 50;
// tableView.backgroundColor = [UIColor redColor];
// tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
tableView.separatorColor = [UIColor redColor];
tableView.dataSource = self;
// tableView.contentSize = CGSizeMake(1000, 1000);
tableView.delegate = self;
[self.view addSubview:tableView];
[tableView release];
上面的代碼實作了兩個代理:dataSource和delegate。分别實作不同的方法;
#pragma mark - 設定某一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row == 0) {
return 20;
}
else
{
return 50;
}
}
#pragma mark - 設定區頭自定義
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView * hear = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
hear.backgroundColor = [UIColor redColor];
return [hear autorelease];
}
#pragma mark - 設定區頭高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
#pragma mark - 選中某行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 頁面切換
if(indexPath.row == 0 && indexPath.section == 0)
{
SecondViewController * secondVC = [[SecondViewController alloc]init ];
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
}
NSLog(@"選中某一行");
}
// ====================================
#pragma mark - 每一個分區的行數
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//NSLog(@"一共10行");
return 2;
}
#pragma mark - 建立cell以及重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
/*
tableView重用機制:當cell移除螢幕時,會被放到tableView的重用隊列中,當顯示cell時,tableView會根據重用辨別在重用隊列中取出cell,顯示到視圖上;
如果tableView能夠顯示n個cell,那麼在tableView建立時,會建立n個cell,當移動時(上一個cell移出一半,下一個cell顯示一部分時)會建立第n+1個cell;
如果移動很快,還可能建立n+2,n+3....個cell,因為移動過快,tableView還來不及從重用隊列取出重用cell,就需要顯示,是以要建立;
*/
// 現檢視是否有重用的cell,如果沒有,需要建立,如果有,不需要建立。
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AA"];//辨別
static int count = 0;
// 如果沒有,需要建立
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AA"]autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
count ++;
NSLog(@"建立%d個cell",count);
}
else
{
NSLog(@"重用");
}
cell.textLabel.text = @"郭仔來啦";
UIImage *img = [UIImage imageNamed:@"bd_logo1.png"];
cell.imageView.image = img;
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// NSLog(@"為每一行提供cell:row:--%d,section:--%d",indexPath.row,indexPath.section);
// UIImage *img = [UIImage imageNamed:@"bd_logo1.png"];
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AA"];
cell.imageView.image = img;
//
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"AA"];
// // 設定cell的選中樣式
// cell.selectionStyle = UITableViewCellSelectionStyleDefault;
// // 設定輔助視圖樣式
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// if (indexPath.row%2 == 0) {
// cell.textLabel.text = @"A";
// }
// else
// {
// cell.textLabel.text = @"B";
// }
// // cell.textLabel.text = @"A";
// cell.detailTextLabel.text = @"XXX";
// cell.imageView.image = img;
return cell;
}
#pragma mark ----------------
#pragma mark - 設定tableView分區數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
#pragma mark - 設定區頭标題
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"A";
}
else
{
return @"B";
}
// return @"區頭";
}
#pragma mark - 設定區尾
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return @"區尾";
}
#pragma mark - 設定右側分區索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray * array = @[@"A",@"B"];
return array;
}
以上代碼是兩種代理中部分方法的實作。
// =========================
小結:
tableView有2種樣式:plain和grouped。 由datasource提供要顯⽰示的資料,delegate提供輔助設定。 系統提供4中樣式的cell。 tableView的重⽤用機制極⼤大提升了性能。