- 添加索引條sectionIndex
- cell分割線
- 沒有數(shù)據(jù)的cell不顯示
- 高度自動計算
- cell右側小圖標
- tableView數(shù)據(jù)刷新
- cell左滑刪除(系統(tǒng))
- cell左滑刪除(自定義)
- 組頭、組尾
- 表頭、表尾
添加索引條sectionIndex
//只要實現(xiàn)這個代理方法,就可以了
//(抽取數(shù)組中相同元素)
-(NSArray<NSString *> *)sectionIndexTitlesForTableView: (UITableView *)tableView{
return [ self . dataSourceArray valuesForKeyPath : @"title" ];
}
//索引文字顏色
self . tableView . sectionIndexColor = [UIColor redColor];
//索引條背景顏色
self . tableView . sectionIndexBackgroundColor = [UIColor yellowColor];
cell分割線
1.設置分割線的顏色:tableView.separatorColor = [ UIColor redColor ];
2.設置分割線樣式: tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
沒有數(shù)據(jù)的cell不顯示
1. tableView.tableFooterView = [ [ UIview alloc] init ];
2. 將分割線樣式設置成none,將tableView的style設置成Grounded
高度自動計算
_tabelView2.rowHeight = UITableViewAutomaticDimension;
_tabelView2.estimatedRowHeight = 200;
cell右側小圖標
cell . accessoryType = UITableViewCellAccessoryCheckmark ; //勾
cell.accessoryView = [[UISwitch alloc]init]; //自定義小控件
tableView數(shù)據(jù)刷新
//整體刷新
[ self . tabelView reloadData] ;
NSArray *indexPaths = @[ [NSIndexPath indexPathforRows : 0 insection : 0] ] ;//獲得indexPathss
//刷新 ,使用前提,數(shù)組總個數(shù)不能改變
[ self . tabelView reloadRowsAtIndexPaths : indexpaths withRowAnimation : UITableViewRowAnimationRight ] ;
//添加刷新
[ self . tabelView insertRowsAtIndexPaths : indexPaths withRowAnimation : UITableViewRowAnimationRight ] ;
//刪除刷新
[ self . tabelView deleteRowsAtIndexPaths : indexPaths withRowAnimation : UITableViewRowAnimationRight ] ;
cell左滑刪除(系統(tǒng))
-(void)tableView : (UITableView * ) tableView commitEditingStyle : ( UITableViewCellEditingStyle ) editingStyle forRowAtIndexPath : (NSIndexPath *)indexpath{
return @"只會監(jiān)聽下面的系統(tǒng)默認的delete方法"
/*刪除操作*/
}
-(NSString *)tableView : (UITableView * ) tableView titleForDeleteConfirmationButtonForRowAtIndexPath : ( NSIndexPath * )indexPath{
return @" 刪除 " ;
}
cell左滑刪除(自定義)
14.熟悉cell 左滑動 編輯或刪除
Cell中直接子控件是 contentView(紅色), contentView的子控件是一個ImageView和兩個UIlabel
//可以進行編輯
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewRowAction *saveAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"保存" handler:^ (UITableViewRowAction *_Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"關注,想回去得刷新表格,或者將tableView的設置為非編輯模式");
self . tableViedw . editing = NO; }];
saveAction.backgroundColor = [UIColor purpleColor];
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"刪除" handler:^ (UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
NSLog(@"刪除");
[self.dataSource removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; }];
deleteAction.backgroundColor = [UIColor orangeColor];
return @[deleteAction,saveAction];
}
組頭、組尾
//組頭(系統(tǒng))
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
MySectionHeaderView *header = [[MySectionHeaderView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_tb.frame), 100)];
header.tag = section;
[header addTarget:self action:@selector(expend:) forControlEvents:UIControlEventTouchUpInside];
return header;
}
//組尾(自定義)
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView *)view;
headerView.backgroundColor = kApp_TintColor;
headerView.textLabel.textColor = [UIColor redColor];
headerView.textLabel.font = [UIFont systemFontOfSize:12.0];
}
表頭、表尾
_tb.tableFooterView = view;(View為任何控件,在第一個組頭上面,最后一個組尾下面互不影響)
//登錄 / 注銷按鈕
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 44)];
_button = [[UIButton alloc] initWithFrame:CGRectMake((kScreen_Width - 200) / 2 , 4, 200, 36)];
[_button setBackgroundColor:[UIColor redColor]];
[view addSubview:_button];
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
_button.layer.cornerRadius = 18;
_button.clipsToBounds = YES;
[_button setTitle:@"賬號管理" forState:UIControlStateNormal];
// [_button setTitle:@"注銷" forState:UIControlStateSelected];
[_button addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
_tb.tableFooterView = view;