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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,527评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,314评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,535评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,006评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,961评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,220评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,664评论 3 392
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,351评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,481评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,397评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,443评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,123评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,713评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,801评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,010评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,494评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,075评论 2 341

推荐阅读更多精彩内容

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