UITableView

目录

  • 设置协议
  • 创建cell
  • 用nib创建cell
  • UITableViewDataSource
  • UITableViewDelegate
  • 常用属性
  • 不常用属性
  • TableView滚动时调用的方法
  • titles索引列

UITableView 继承 UIScrollView
//获取选中的cell
self.myTableView.indexPathForSelectedRow.row

设置协议

1.创建tableView对象

 UITableViewStylePlain,  (默认)扁平风格(也是可以分组)
 UITableViewStyleGrouped  分组

 tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
 //2.显示在界面上
 [self.view addSubview:tableView];
补充:UICollectionView底部被标签了控制器(tabBar)遮挡的解决办法
//
 self.collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height-44) collectionViewLayout:flowLayout];
    
 self.automaticallyAdjustsScrollViewInsets = NO;
 self.collectionV.contentInset = UIEdgeInsetsMake(44, 0, 0, 0);

3.设置代理
数据相关的代理(只有实现dataSource的协议方法,才能在tableView上去显示数据)

//声明协议
 @interface ViewController ()<UITableViewDataSource>
  
//设置代理
 tableView.dataSource = self;

UITableViewDataSource

必须实现的协议方法

设置每组的个数

返回值:设置分组中的行数(每组cell的个数)
参数1:委托
参数2:第几组

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    //需要每一组显示100条数据
    return 100;
}
Cell(tableView显示数据,也不是通过tableView本身去显示数据,而是通过cell来显示)

返回值:创建好的cell
参数1:委托
参数2:cell的位置(和坐标没有关系,只和组和行有关)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

  !!!!!面试过程中常问的问题:
  1.去复用池中查看是否有可以复用(重复使用)的cell;如果有就返回可以复用的cell地址,没有返回nil
   参数:复用ID
 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
    
  2.判断是否拿到可以复用cell,如果没有拿到就创建一个新的cell(最终创建cell的个数是整屏显示的cell的个数加1或者加2)
    if (cell==nil) {
    参数1:风格
    参数2:复用ID

   cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellId"];
    }

  3.刷新数据(刷新cell上显示的内容)
   indexPath由section(组)和row(行)组成
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld组,第%ld行", indexPath.section, indexPath.row];
    
   4.返回cell
   return cell;
    
}

设置cell的选中样式

//UITableViewCellSelectionStyleNone没有选中的效果
 [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

nib定制Cell

让TableView先注册nib

 // 注册单元格
 [_tableView registerNib:[UINib nibWithNibName:@"SubjectCell" bundle:nil] forCellReuseIdentifier:SubjectCellIdentifier];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  // 复用单元格
  SubjectCell * cell = [tableView dequeueReusableCellWithIdentifier:SubjectCellIdentifier forIndexPath:indexPath];
    
  // 获取数据模型
  SubjectModel * model = self.subjectArray[indexPath.row];
    
  cell.model = model;
    
  return cell;
}

设置tableView的分组数(默认是1个分组)
参数:委托

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 3;  
}

可选协议

设置header的标题

- ( NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    switch (section) {
        case 0:
            return @"热门";
            break;
            
        case 1:
            return @"最近更新";
            break;
        
        case 2:
            return @"销量最高";
            break;
            
        default:
            break;
    }
    return @"头标题";
}

设置footer的标题

- ( NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{

    return @"脚标题";
    
}

UITableViewDelegate

MARK高度相关

设置Cell行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

        return 50; 
}
选中相关

选中一个cell的时候会调用这个方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"cell被选中");
    //在这儿跳转到下一个界面
    NextViewController * next = [[NextViewController alloc] init];
    
    //传值
    next.indexpath = indexPath;
    
    
    //push到下一个界面
    [self.navigationController pushViewController:next animated:YES];
    
    //present到下一个界面
    [self presentViewController:next animated:YES completion:nil];
    
}
头视图和脚视图相关

设置header高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 50 * section + 10;
    return 50;
}

设置Footer高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    return 50;
}

设置每一组的headerView

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

   //创建一个视图(设置frame无效)
   UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
   //设置背景颜色
   headerView.backgroundColor = [UIColor redColor];
        
   //创建一个label
   UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
   label.text = [NSString stringWithFormat:@"头标题:%ld", section];
   [headerView addSubview:label];
        
   return headerView;
    
}

设置每组脚视图Footer

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    //创建一个视图(设置frame无效)
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    //设置背景颜色
    headerView.backgroundColor = [UIColor yellowColor];
    
    
    //创建一个label
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(30, 0, 200, 50)];
    label.text = @"脚标题";
    [headerView addSubview:label];
    
    
    return headerView;
    
}

Cell的附件相关

设置指定位置的cell的附件类型

  cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    UITableViewCellAccessoryNone,(默认)
    UITableViewCellAccessoryDisclosureIndicator 一个小箭头(常用)
    UITableViewCellAccessoryDetailDisclosureButton 一个详情按钮加小箭头
    UITableViewCellAccessoryCheckmark,  一个蓝色勾
    UITableViewCellAccessoryDetailButton  一个详情按钮
      
添加完箭头以后,若还想在箭头前面添加文字

http://www.th7.cn/Program/IOS/201410/289058.shtml


cell.detailTextLabel.text = @"mona";
如果你执行之后可以看到箭头前有文字出现的话,那么恭喜你,你是幸运的。
我就没那么幸运,改了很多次之后都cell右侧都没有显示过文字出来,后来就根据这个情况,找了很久都没找到相应的解决方法。
后来在一段demo中才知道,原来要在右侧显示出detailTextLabel的文字,还需要在cellForRowAtIndexPath:中把cell的类型设置为

UITableViewCellStyleValue1,cellForRowAtIndexPath:的整段代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"formCell"];    
 if (cell == nil) {       
 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"formCell"];       
 cell.textLabel.text = @"monalisa";   
  }    
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text = @"mona";

附件上的按钮被点击后调用这个方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"附件按钮被点击");
}
将要显示一个cell的时候会调用这个方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{

    NSLog(@"将要显示cell");
}

常用属性

1.行高(每一行的高度都是200)

 tableView.rowHeight = 200;

2.分组的header和footer的高度

 tableView.sectionHeaderHeight = 100;
 tableView.sectionFooterHeight = 100;

3.设置分割线的样式

 UITableViewCellSeparatorStyleNone,     (隐藏分割线)
 UITableViewCellSeparatorStyleSingleLine,
 UITableViewCellSeparatorStyleSingleLineEtched
   
 [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];

4.手动刷新数据(自动刷新是在创建和滑动tableView的时候)---刷新会重新调用dataSource中创建cell的方法去重新创建cell

//刷新所有的cell
[tableView reloadData];


 //刷新指定的组
 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:5];
 [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//刷新指定的row
[self.tableView reloadRowsAtIndexPaths:<#(nonnull NSArray<NSIndexPath *> *)#> withRowAnimation:<#(UITableViewRowAnimation)#>]

5.将视图添加到Header上,只能添加一个

 UIView * view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
 
 view.backgroundColor = [UIColor yellowColor];
    [tableView setTableHeaderView:view];

6.设置内容偏移

[tableView setContentInset:UIEdgeInsetsMake(100, 0, 0, 0)];

7.获取cell在当前TabView中的indexPath

 NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

8.点击后取消cell选中效果


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 {    
      // 取消选中状态  
      [tableView deselectRowAtIndexPath:indexPath animated:NO];
 }

不常用属性

1.设置分割线的边距

[tableView setSeparatorInset:UIEdgeInsetsMake(50, 50, 50, 50)];

2.设置分割线的颜色

  [tableView setSeparatorColor:[UIColor redColor]];

3.获取指定的坐标点所在的位置(第多少组第几行)

 - ( NSIndexPath *)indexPathForRowAtPoint:(CGPoint)point;

4.获取到指定位置(第几组第几行)对应的cell

 - ( UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

5.获取当前界面上可见的所有的cell

 //@property (nonatomic, readonly) NSArray *visibleCells;

6.d

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

6.滚动到指定位置
参数1: 指定的位置
参数2: 滚动位置

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

7.将选中的cell滚动到指定位置(顶部底部或者中间)

- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

8.刷新指定位置的cell

- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

9.选中指定的位置

- (void)selectRowAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition;

TableView滚动时调用的方法

当TableView滚动时会调用该方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

当TableView停止滚动时会调用该方法

 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;

titles索引列

//设置背景颜色
 self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
//设置字体颜色 
self.tableView.sectionIndexColor = [UIColor blackColor];


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return self.indexArray;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return index;
}

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

推荐阅读更多精彩内容