tableView技巧

2016-11-18  本文已影响62人  Hyman0819

添加索引条sectionIndex

//只要实现这个代理方法,就可以了
//(抽取数组中相同元素)
 -(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;

没有数据的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数据刷新

//整体刷新
[ self . tabelView  reloadData] ;
 NSArray  *indexPaths  =  @[  [NSIndexPath  indexPathforRows : 0  insection : 0]  ] ;//获得indexPathss
//刷新 ,使用前提,数组总个数不能改变
[ self . tabelView  reloadRowsAtIndexPaths : indexpaths withRowAnimation : UITableViewRowAnimationRight ] ;
//添加刷新
 [ self . tabelView  insertRowsAtIndexPaths : indexPaths withRowAnimation : UITableViewRowAnimationRight ] ;
 //删除刷新
[ self . tabelView  deleteRowsAtIndexPaths : indexPaths withRowAnimation : UITableViewRowAnimationRight ] ;

cell左滑删除(系统)

-(void)tableView : (UITableView * ) tableView commitEditingStyle : ( UITableViewCellEditingStyle ) editingStyle forRowAtIndexPath : (NSIndexPath  *)indexpath{ 
                              return  @"只会监听下面的系统默认的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];
 }

组头、组尾

//组头(系统)
-(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;
上一篇下一篇

猜你喜欢

热点阅读