//tableView的創建
//1.初始化 initWithFrame:style:(plian,gronp)
//2.設置屬性(行高, 分割線, 表頭, 表尾)
//3.添加到父視圖
//4.釋放
//tableView顯示數據
//1.設置dataSource
//2.遵循協議<UITableViewDateSource>
//3.實現兩個必須要實現的方法(a. 返回某個分區行數, b.返回cell)
//tableViewCell 創建
//1.重用標識符(static初始化一次,節約內存)
//2.通過標識符去復用池找cell
//3.判斷cell是否找到, 如果沒有找到, 創建cell(initWithStyle:reuseIdentifier)要釋放autorelease
//4.判斷括號外,對cell進行復制
//5.return cell
//
UITableViewDelegate, UITableViewDataSource一部分代理方法的使用
#import "TableViewController.h"
@interface TableViewController ()<UITableViewDelegate, UITableViewDataSource>//tableView想要實現顯示內容必須遵守的協議
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Cell";
self.view.backgroundColor = [UIColor yellowColor];
//tableView的創建
//1.初始化
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
//2.設置屬性(行高, 分割線, 表頭, 表尾)
tableView.rowHeight = 60;
//UITableViewCellSeparatorStyleSingleLine(默認)
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tableView.separatorColor = [UIColor redColor];
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
tableView.backgroundColor = [UIColor grayColor];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 375, 30)];
headerLabel.text = @"IOS應用排行榜";
headerLabel.textAlignment = NSTextAlignmentCenter;
tableView.tableHeaderView = headerLabel;//表頭,需要先創建一個Label,然后把創建好的Label賦給表頭表尾
[headerLabel release];
//通過表尾可以清除多余分割線
//tableView.tableFooterView = [[[UIView alloc] init] autorelease];
//設置是否允許多選
tableView.allowsMultipleSelection = YES;
//tableView,想要顯示數據,使用dataSource模式, dataSource實質還是代理模式
//步驟a.設置dataSource
// b.遵循協議<UITableViewDateSource>
// c.實現兩個必須要實現的方法(a.返回某個分區行數, b.返回cell)
tableView.dataSource = self;
tableView.delegate = self;
//3. 添加父視圖
[self.view addSubview:tableView];
//4. 釋放
[tableView release];
// Do any additional setup after loading the view.
}
#pragma mark -- UITableViewDataSource
//UITableViewDataSource中方法
//@required 必須要實現的兩個方法1.和 2.
//1.設置行數(如果有分區的話,可以通過if判斷每個分區,然后給每個分區賦不同的行數)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;//此處返回設置10行,返回10;
}
//2.UITableViewCell, 單元格類, 繼承於UIView, 用於在UITableView上顯示內容
//注:會執行多次, 每走一次, 創建一個cell; 第一次只創建出一個屏幕能夠顯示的cell,如果滾動tableView, 會再走這個方法,再次創建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell重用機制(復用機制),用於降低內存消耗
//方法內部的實現
//a.定義重用標示,static 靜態變量,初始化一次, 降低內存的消耗
static NSString *identifier = @"CELL";
//b.去重用隊列中根據標識符取可重用的 cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//c. 判斷是否獲取到可重用的 cell( 最后要空間釋放 )
if (!cell) {//!cell 相當於 cell == nil
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//輔助標示類型
}
//顯示內容
cell.imageView.image = [UIImage imageNamed:@"李四"];
cell.textLabel.text = @"李四";
cell.detailTextLabel.text = @"我是李四";
return cell;
}
/*
//輔助表示類型 用accessoryType 賦值
UITableViewCellAccessoryNone, // don't show any accessory view
UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks
UITableViewCellAccessoryCheckmark, // checkmark. doesn't track
UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks
*/
//@optional UITableViewDataSource 中不必實現但是經常用到的方法
//1.設置分組個數section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;//返回多少就是多少個分區
}
//2.設置區頭(section)
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSArray *array = @[@"A", @"B", @"C"];
return array[section];
}
//3.設置區尾
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
NSArray *array = @[@"AAA", @"BBB", @"CCC"];
return array[section];
}
//4.右側添加一個索引表
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
NSArray *aa = @[@"A", @"B", @"C"];
return aa;
}
#pragma mark - UITableViewDelegate
//UITableViewDelegate中得一些常用方法
//1.將顯示單元格
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s", __FUNCTION__);
}
//2.自定義區頭視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *hearderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
hearderView.backgroundColor = [UIColor yellowColor];
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
// label.text = @"zhao";
// [hearderView addSubview:label];
// [label release];
return [hearderView autorelease];
}
//3.自定義區尾視圖
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//
//}
//4.設置每行間隔的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
//5.選擇哪一section的哪一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section: %ld, row: %ld", indexPath.section, indexPath.row);
}
//6.設置選中的行所執行的動作
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"%s",__FUNCTION__);
//NSUInteger row = [indexPath row];
return indexPath;
}
//設置讓UITableView行縮進
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%s",__FUNCTION__);
NSUInteger row = [indexPath row];
return row;
}
@end