美文网首页
tableView技巧

tableView技巧

作者: Hyman0819 | 来源:发表于2016-11-18 19:14 被阅读62次
  • 添加索引条sectionIndex
  • cell分割线
  • 没有数据的cell不显示
  • 高度自动计算
  • cell右侧小图标
  • tableView数据刷新
  • cell左滑删除(系统)
  • cell左滑删除(自定义)
  • 组头、组尾
  • 表头、表尾

添加索引条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;

相关文章

  • tableView技巧

    添加索引条sectionIndex cell分割线 没有数据的cell不显示 高度自动计算 cell右侧小图标 t...

  • TableView技巧

    隐藏cell分割线_tableView.separatorStyle = NO; 让单元格之间有距离 设置分割线左...

  • 奥义-完美cell封装术

    导语: TableView的使用存在很多的技巧,如果能够巧妙的使用和封装TableView,代码的可读性和优美度会...

  • tableView 小技巧

    1.tableview section头部不悬停正常section不为0的时候,section的headerVie...

  • tableView小技巧

    重写scrollView最上方的偏移量(默认向下偏移64) if([selfrespondsToSelector:...

  • TableView小技巧

    在项目的时候,发现一个问题就是在Cell上的控件有背景色时,用户选择了这个cell,这个cell上控件的背景色,会...

  • tableView常用技巧

    cell 点击背景颜色 设置 (UITableViewCell *)tableView:(UITableView ...

  • tableView使用技巧

    UITableView 只更新一个cell 或某部分sectionhttps://www.jianshu.com/...

  • iOS 多选删除(附tableViewTips及单选删除)

    一、前言 这次分享并记录一下tableView的多选删除,并额外记录一下单选删除及tableView的设置小技巧。...

  • iOS 开发中常用的技巧

    iOS开发中常用但经常忘记的技巧 1、 隐藏tableViewCell的分割线:tableView.separat...

网友评论

      本文标题:tableView技巧

      本文链接:https://www.haomeiwen.com/subject/rnrspttx.html