目錄
- 設置協議
- 創建cell
- 用nib創建cell
- UITableViewDataSource
- UITableViewDelegate
- 常用屬性
- 不常用屬性
- TableView滾動時調用的方法
- titles索引列
UITableView 繼承 UIScrollView
//獲取選中的cell
self.myTableView.indexPathForSelectedRow.row
設置協議
1.創建tableView對象
UITableViewStylePlain, (默認)扁平風格(也是可以分組)
UITableViewStyleGrouped 分組
tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
//2.顯示在界面上
[self.view addSubview:tableView];
補充:UICollectionView底部被標簽了控制器(tabBar)遮擋的解決辦法
//
self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-44) collectionViewLayout:flowLayout];
self.automaticallyAdjustsScrollViewInsets = NO;
self.collectionV.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);
3.設置代理
數據相關的代理(只有實現dataSource的協議方法,才能在tableView上去顯示數據)
//聲明協議
@interface ViewController ()<UITableViewDataSource>
//設置代理
tableView.dataSource = self;
UITableViewDataSource
必須實現的協議方法
設置每組的個數
返回值:設置分組中的行數(每組cell的個數)
參數1:委托
參數2:第幾組
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//需要每一組顯示100條數據
return 100;
}
Cell(tableView顯示數據,也不是通過tableView本身去顯示數據,而是通過cell來顯示)
返回值:創建好的cell
參數1:委托
參數2:cell的位置(和坐標沒有關系,只和組和行有關)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
!!!!!面試過程中常問的問題:
1.去復用池中查看是否有可以復用(重復使用)的cell;如果有就返回可以復用的cell地址,沒有返回nil
參數:復用ID
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
2.判斷是否拿到可以復用cell,如果沒有拿到就創建一個新的cell(最終創建cell的個數是整屏顯示的cell的個數加1或者加2)
if (cell==nil) {
參數1:風格
參數2:復用ID
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
}
3.刷新數據(刷新cell上顯示的內容)
indexPath由section(組)和row(行)組成
cell.textLabel.text = [NSString stringWithFormat:@"第%ld組,第%ld行", indexPath.section, indexPath.row];
4.返回cell
return cell;
}
設置cell的選中樣式
//UITableViewCellSelectionStyleNone沒有選中的效果
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
nib定制Cell
讓TableView先注冊nib
// 注冊單元格
[_tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:SubjectCellIdentifier];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 復用單元格
SubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:SubjectCellIdentifier forIndexPath:indexPath];
// 獲取數據模型
SubjectModel * model = self.subjectArray[indexPath.row];
cell.model = model;
return cell;
}
設置tableView的分組數(默認是1個分組)
參數:委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
可選協議
設置header的標題
- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return @"熱門";
break;
case 1:
return @"最近更新";
break;
case 2:
return @"銷量最高";
break;
default:
break;
}
return @"頭標題";
}
設置footer的標題
- ( NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"腳標題";
}
UITableViewDelegate
MARK高度相關
設置Cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
選中相關
選中一個cell的時候會調用這個方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"cell被選中");
//在這兒跳轉到下一個界面
NextViewController * next = [[NextViewController alloc] init];
//傳值
next.indexpath = indexPath;
//push到下一個界面
[self.navigationController pushViewController:next animated:YES];
//present到下一個界面
[self presentViewController:next animated:YES completion:nil];
}
頭視圖和腳視圖相關
設置header高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// return 50 * section + 10;
return 50;
}
設置Footer高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 50;
}
設置每一組的headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//創建一個視圖(設置frame無效)
UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//設置背景顏色
headerView.backgroundColor = [UIColor redColor];
//創建一個label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
label.text = [NSString stringWithFormat:@"頭標題:%ld", section];
[headerView addSubview:label];
return headerView;
}
設置每組腳視圖Footer
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//創建一個視圖(設置frame無效)
UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
//設置背景顏色
headerView.backgroundColor = [UIColor yellowColor];
//創建一個label
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
label.text = @"腳標題";
[headerView addSubview:label];
return headerView;
}
Cell的附件相關
設置指定位置的cell的附件類型
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UITableViewCellAccessoryNone,(默認)
UITableViewCellAccessoryDisclosureIndicator 一個小箭頭(常用)
UITableViewCellAccessoryDetailDisclosureButton 一個詳情按鈕加小箭頭
UITableViewCellAccessoryCheckmark, 一個藍色勾
UITableViewCellAccessoryDetailButton 一個詳情按鈕
添加完箭頭以后,若還想在箭頭前面添加文字
http://www.th7.cn/Program/IOS/201410/289058.shtml
cell.detailTextLabel.text = @"mona";
如果你執行之后可以看到箭頭前有文字出現的話,那么恭喜你,你是幸運的。
我就沒那么幸運,改了很多次之后都cell右側都沒有顯示過文字出來,后來就根據這個情況,找了很久都沒找到相應的解決方法。
后來在一段demo中才知道,原來要在右側顯示出detailTextLabel的文字,還需要在cellForRowAtIndexPath:中把cell的類型設置為
UITableViewCellStyleValue1,cellForRowAtIndexPath:的整段代碼如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"formCell"];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"formCell"];
cell.textLabel.text = @"monalisa";
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = @"mona";
附件上的按鈕被點擊后調用這個方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
NSLog(@"附件按鈕被點擊");
}
將要顯示一個cell的時候會調用這個方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"將要顯示cell");
}
常用屬性
1.行高(每一行的高度都是200)
tableView.rowHeight = 200;
2.分組的header和footer的高度
tableView.sectionHeaderHeight = 100;
tableView.sectionFooterHeight = 100;
3.設置分割線的樣式
UITableViewCellSeparatorStyleNone, (隱藏分割線)
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
4.手動刷新數據(自動刷新是在創建和滑動tableView的時候)---刷新會重新調用dataSource中創建cell的方法去重新創建cell
//刷新所有的cell
[tableView reloadData];
//刷新指定的組
NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:5];
[self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
//刷新指定的row
[self.tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]
5.將視圖添加到Header上,只能添加一個
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
view.backgroundColor = [UIColor yellowColor];
[tableView setTableHeaderView:view];
6.設置內容偏移
[tableView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];
7.獲取cell在當前TabView中的indexPath
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
8.點擊后取消cell選中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消選中狀態
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
不常用屬性
1.設置分割線的邊距
[tableView setSeparatorInset:UIEdgeInsetsMake(50, 50, 50, 50)];
2.設置分割線的顏色
[tableView setSeparatorColor:[UIColor redColor]];
3.獲取指定的坐標點所在的位置(第多少組第幾行)
- ( NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;
4.獲取到指定位置(第幾組第幾行)對應的cell
- ( UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
5.獲取當前界面上可見的所有的cell
//@property (nonatomic, readonly) NSArray *visibleCells;
6.d
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
6.滾動到指定位置
參數1: 指定的位置
參數2: 滾動位置
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
7.將選中的cell滾動到指定位置(頂部底部或者中間)
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
8.刷新指定位置的cell
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation
9.選中指定的位置
- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;
TableView滾動時調用的方法
當TableView滾動時會調用該方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
當TableView停止滾動時會調用該方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
titles索引列
//設置背景顏色
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//設置字體顏色
self.tableView.sectionIndexColor = [UIColor blackColor];
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.indexArray;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *key = [self.indexArray objectAtIndex:section];
return key;
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return index;
}