tableView 的使用步驟和方法

常用小方法
自定義區頭視圖
- (UIView )tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section;
{

  • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
    return 25;
    }

單元格被選中後接着被取消
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}

單元格不能被選中
cell.selectionStyle = UITableViewCellSelectionStyleNone;

一般步奏
第一步 初始化表視圖
- (void)loadView // 加載視圖用
{
// 創建一個表視圖 tableView 賦值給 controller.view
UITableView *tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:(UITableViewStylePlain)];
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//分割線樣式
tableView.separatorColor = [UIColor yellowColor];//分割線顏色
// 設置頁眉與頁腳
UIImageView *header = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”/Users/lanou3g/Desktop/N%07T(F}L8%MRYLR}XD25{U.jpg”]];
UIImageView *header1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@”/Users/lanou3g/Desktop/N%07T(F}L8%MRYLR}XD25{U.jpg”]];
tableView.tableHeaderView = header; // 頁眉想要偏移 先定義UIImageView的 fram 然後給tableHeaderView = 一個空視圖 再使用 addsubview
tableView.tableFooterView = header1; // 頁腳想要偏移先定義UIImageView的 fram 然後直接傳給tableFooterView
// *給 tableView 添加代理和數據源 .h 裏添加代理
tableView.dataSource = self;
tableView.delegate = self;
self.view = tableView; // 此處用等號
[tableView release];

}
創建數據
- (void)setData
{
NSArray *zArray = @[@”趙玉鳳”,@”趙慧珍”,@”張文澤” ];
NSArray *yArray = @[@”易慧雲”,@”瑤瑤”];
NSArray *xArray = @[@”謝爲民”];
NSArray *wArray = @[@”吳淑敏”];
self.teacherDic = @{@”Z”:zArray,@”Y”:yArray,@”X”:xArray,@”W”:wArray};
self.keysArry = [[_teacherDic allKeys]sortedArrayUsingSelector:@selector(compare:)];
}
第二步 確定有幾個分組
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView// 告訴表視圖有幾個分區
{
// 根據聯繫人組數
return [_keysArry count];
}

第三步 確定各個分區有幾個單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section// 告訴表視圖各個分區都有幾個單元格 section 表示第幾個分組
{
// 1.取到對應的著名
NSString *groupName = _keysArry[section];
//2.根據著名
NSArray *array = _teacherDic[groupName];
return [array count];
//return [_teacherDic[_keysArry[section]] count];
}
第四步 確定單元格高度
// 單元格高度 自適應高度
- (CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath
{
// if (0 == indexPath.section) {
// return 100;
// }
}

第五步 生成單元格
- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath// 生成單元格
{
// 第一步先判斷可重用單元格隊列裏取可以重用的單元格.
// 第二步 創建一個全局區的變量 static 修飾的 NSString作爲重用標誌
static NSString *cellIndentifier = @”cell1”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
// 當沒有可以被重用的單元格時創建新單元格加重用標記
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:cellIndentifier];
}
// 文本框 左側單元格編號
// cell.textLabel.text = [NSString stringWithFormat:@”%ld”,indexPath.row];
// 左側加載圖片
UIImage *image = [UIImage imageNamed:@”/Users/lanou3g/Desktop/PNG/5845/180.png”];
cell.imageView.image = image;
//1. 取分區對應的聯繫人組名
NSString *groupName = _keysArry[indexPath.section];
// 2.獲取組名所對應的聯繫人組
NSArray *array = _teacherDic[groupName];
//3.取 row 對應的聯繫人
NSString *name = array[indexPath.row];
//4.給 cell 賦值
cell.textLabel.text = name;
return cell;
}
第六步 設置區頭標題
// 區頭標題
- (NSString )tableView:(UITableView )tableView titleForHeaderInSection:(NSInteger)section
{
// 獲取分區下標對應的分組名
NSString *key = _keysArry[section];
return key;
}
第七步 索引標題
// 右側分區索引標題
- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.keysArry;
}
第八步 點擊方法
// 選中單元格之後,執行 裏面的方法
- (void)tableView:(UITableView )tableView didSelectRowAtIndexPath:(NSIndexPath )indexPath
{
NSLog(@”asfesaefsfe”);
}
第九步

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章