一 UITableViewspa
//UITableView 繼承於UIScrollView代理
//咱們以後看到的全部可以本身滑動的視圖的控件 所有都繼承於UIScrollView的orm
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];繼承
//鋪放數據源的代理設置 須要遵照UITableViewDataSource協議圖片
tableView.dataSource = self;string
//一些輔助性的代理方法 調整tableView樣式 以及每個cell的點擊方法it
tableView.delegate = self;io
//設置沒有分割線table
tableView.SeparatorStyle = UITableViewCellSeparatorStyleNone;class
//設置每一行的高度----一般狀況下只在方法中來設立行高
tableView.rowHeight = 100;
//設置分區表頭高度
tableView.sectionHeaderHeight = 100;
//設置分區表尾高度
tableView.sectionFooterHeight = 100;
#pragma mark --- 返回多少分區 默認是1個---
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
#pragma mark --- 必須實現的 每一個分區下 返回多少行 ---
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// 每一個分區下 返回不一樣的行數
/*
if (section == 0)
{
return 20;
}
else if(section == 1)
{
return 9;
}
return 13;
*/
return 20;
}
#pragma mark --- 返回cell的方法 必須實現的 ---
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]autorelease];
static NSString *cellIndentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIndentifier];
}
// 設置主標題
cell.textLabel.text = [NSString stringWithFormat:@"第%ld分區", indexPath.section];
// 設置右邊的輔助視圖
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// 設置副標題
cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];
// 設置點擊渲染顏色
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// 自定義點擊渲染顏色
// 前提條件:渲染顏色不能設置成 none
/*
UIView *myView = [[UIView alloc]init];
myView.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView = myView;
[myView release];
*/
// 設置圖片
cell.imageView.image = [UIImage imageNamed:@"0.png"];
// 輔助視圖設置成本身的圖片
/*
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
imageView.image = [UIImage imageNamed:@"0.png"];
cell.accessoryView = imageView;
[imageView release];
*/
return cell;
}
/*
#pragma mark --- 設置每行高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0)
{
if (indexPath.row == 0)
{
return 200;
}
else
{
return 100;
}
}
return 50;
}
*/
#pragma mark --- 點擊cell的方法 ---
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"點擊了 %ld分區 的 %ld行", indexPath.section, indexPath.row);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
}
/*
#pragma mark --- 返回右邊的標題 ---
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return @[@"A", @"B", @"C"];
}
#pragma mark --- 返回每一個分區的標題 ---
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
return @"A";
}
else if (section == 1)
{
return @"B";
}
return @"C";
}
*/
/*
#pragma mark --- 分區表頭高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50;
}
#pragma mark --- 自定義分區表頭 ---
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *myView = [[[UIView alloc]init]autorelease];
myView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"第%ld分區表頭", section];
[myView addSubview:label];
[label release];
return myView;
}
#pragma mark --- 設置分區表尾高度 ---
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 50;
}
#pragma mark --- 自定義分區表尾 ---
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *myView = [[[UIView alloc]init]autorelease];
myView.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 200, 30)];
label.backgroundColor = [UIColor greenColor];
label.text = [NSString stringWithFormat:@"第%ld分區表尾", section];
[myView addSubview:label];
[label release];
return myView;
}