tableView技巧

  • 添加索引条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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 隐藏cell分割线_tableView.separatorStyle = NO; 让单元格之间有距离 设置分割线左...
    Vijay_阅读 2,224评论 0 0
  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,967评论 3 38
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,289评论 4 61
  • 周三A股延续震荡走势,无量,强势股换了一批,总体走势如预期。大盘指数今日平开,早盘小幅冲高后开始震荡下行,尾盘小幅...
    牛人看股阅读 2,286评论 0 0
  • April is the cruelest month, breedingLilacs out of the de...
    Lamun_busumda小满阅读 3,440评论 0 0