tableView技巧

  • 添加索引條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;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 隱藏cell分割線_tableView.separatorStyle = NO; 讓單元格之間有距離 設置分割線左...
    Vijay_閱讀 342評論 0 0
  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,290評論 3 38
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,208評論 4 61
  • 周三A股延續(xù)震蕩走勢,無量,強勢股換了一批,總體走勢如預期。大盤指數(shù)今日平開,早盤小幅沖高后開始震蕩下行,尾盤小幅...
    牛人看股閱讀 327評論 0 0
  • April is the cruelest month, breedingLilacs out of the de...
    Lamun_busumda小滿閱讀 565評論 0 0

友情鏈接更多精彩內容