?????
//? 設置tableView的行高
??? self.tableView.rowHeight = 100;
//? 設置tableView分割線的樣式
//? UITableViewCellSeparatorStyleNone 不顯示分割線
//? UITableViewCellSeparatorStyleSingleLine? 顯示分割線(默認)
??? self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
??? self.tableView.allowsSelection = NO; // 不允許選中
//? 設置分割線的顏色
??? self.tableView.separatorColor = [UIColor orangeColor];
?? //? 讓沒有內容單元格不顯示[小技巧]
??? self.tableView.tableFooterView = [[UIView alloc] init];
//? 設置(top,left,bottom,right)[top與bottom無效]
??? self.tableView.separatorInset = UIEdgeInsetsMake(0,10, 0, 10);
在stroyborud 加載cell
??? CZFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"friend"];
??? return cell;
????
dataSource 常用方法
/**
?*? 多少個分組 ?numberOfSectionsInTableView?
?*/
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
??? return 2;
}
/**
?*? 一個分組有多少行 ?numberOfRowsInSection
?*/
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
? ? return 2;
}
/**
?*? 每一個分組顯示什么內容 ?cellForRowAtIndexPath
?*/
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
? ?
//1創建Cell
static NSString *reusedId = @"item";
??? //??? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId forIndexPath:indexPath];--------->wrong!!!!!
??? UITableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:reusedId ];
???
??? if (cell ==nil) {
??????? cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
??? }
// 2.獲取數據
??? LYGroup *group = _groups[indexPath.section];
??? LYItem *item = group.items[indexPath.row];
??? cell.textLabel.text =item.title;
??? cell.imageView.image =[UIImage imageNamed:item.icon];
//3.返回cell
? ? ?return cell;
}
/**
?*? 分組(頭部)標題?titleForHeaderInSection
?*/
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
? ? ? ? ??return @“頭部”;
}
/**
?*? 分組(尾部)描述 ?titleForFooterInSection
?*/
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
? ? ? ? ??return @“尾部”;
}
/**
?*? 分組索引 ?sectionIndexTitlesForTableView
?*/
- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView
{
??? //返回groups數組中,所有group對象的title屬性,返回的是數組
??? return [self.carGroups valueForKeyPath:@"title"];
}
代理常用方法
?*? 設置每一行的行高 ?heightForRowAtIndexPath
?*/
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
??? if (indexPath.row % 2 == 0) {
??????? return 60;
??? }else{
??????? return 100;
??? }
}
/**
?*? 已經選中某一行?didSelectRowAtIndexPath
?*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// ? zd long, long long ,int
??? NSLog(@"用戶選中了第%zd組,第%zd行",indexPath.section,indexPath.row);
}
//當取消選中某一行時候執行 ?didDeselectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
?? NSLog(@"用戶取消選中了第%zd組,第%zd行",indexPath.section,indexPath.row);
}
CELL 常用屬性
//? UITableViewCell *cell = [[UITableViewCell alloc] init];
//? UITableViewCellStyleDefault 只顯示圖標和名稱
//? UITableViewCellStyleSubtitle 顯示圖標,名稱,描述<下面>
//? UITableViewCellStyleValue1 顯示圖標,名稱,描述<后面>
//? UITableViewCellStyleValue2 顯示名稱,描述<后面>
//? 背景視圖color
??? UIView *backView = [[UIView alloc] init];
??? backView.backgroundColor = [UIColor redColor];
// backgroundView的優先級高于backgroundColor
??? cell.backgroundView = backView;
//? 背景顏色
??? cell.backgroundColor = [UIColor blueColor];
??? UIView *selectedView = [[UIView alloc] init];
??? selectedView.backgroundColor = [UIColor blueColor];
//? 選中背景視圖
??? cell.selectedBackgroundView = selectedView;
???
//? 指示器相關
//? 設置指示器的類型
??? cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//? 指示器視圖 ??? cell.accessoryView = [[UISwitch alloc] init];
/**
?*??從緩存中取cell? ? //1創建Cell // 2.獲取數據 //3.返回cell
?*/
// 定義重用標識
? ? static NSString *reuseId = @"heroCell";
???
// 去緩沖池中查找重用的cell
?? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
//? 如果沒有找到可以重用cell就創建新的cell
??? if (cell == nil) {
???????? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseId];
//??????? NSLog(@"創建cell");
??? }
//????? 用來刷新指定的行
??????? [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
?//????? 刷新表格
[self.tableView reloadData];
//????? 滾動讓某個區域可見
[self.tableView scrollRectToVisible: (CGRect)rect animated:YES];
//滾動到哪一行
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.tgs.count -1 inSection:0];
??? [self.tableView scrollToRowAtIndexPath:indexPath?atScrollPosition:(UITableViewScrollPositionMiddle) animated:YES];
?
?//?第一組和最頂部的間距 headerView
??? self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 15)];
??? //?設置 組間距 15
??? self.tableView.sectionHeaderHeight = 15;
??? self.tableView.sectionFooterHeight = 0;
???
??? //設置tableView的背景圖片
??? self.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg"]];
? ? //重寫init方法,設置分組
??? return [super initWithStyle:UITableViewStyleGrouped];
?
UITableViewHeaderFooterView
重寫? [[self alloc] initWithReuseIdentifier:reuseId];
UITableViewCell
重寫 [[self alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];