先来看一下tableview 的结构(plain style)。

--------------------------------------       +

header                           table header

--------------------------------------       +

--------------------------------------        +

section header

--------------------------------------

cell

--------------------------------------         table section

cell

---------------------------------------       +

section footer

---------------------------------------       +

---------------------------------------       +

footer                                    talbe foot

---------------------------------------       +

1、首先要在 UITableViewController 的 viewDidLoad方法中设置  self.tableView.sectionHeaderHeight的值,不然你不会看到section的数据的。

2、在UITableViewController 的numberOfSectionsInTableView方法中返回section的个数。

3、在UITableViewController 的(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法中设置cell中的数据。

  1. static NSString *identifier = @"dataCell";//这个用于cell种类的标识
  2. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //按identifier查找可重用cell。也就是系统要是有identifier类型的cell给你用,就返回这个可用的cell给你,否则返回nil
  3. if (!cell) {//如果没有
  4. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];//老子新建一个
  5.  
  6. }
  7. DataGroupModel *groupModel = self.dataArray[indexPath.section]; //这里的dataArray可以是一个NSMutableArray,包含二级数据,第一级group用于section,第二级data用于cell
  8. DataModel *dataModel = groupModel.data[indexPath.row]; //定为到group的data级数据
  9. cell.imageView.image = [UIImage imageNamed:dataModel.icon];//这里cell的imageView等成员是UITableViewCell自己带的
  10. cell.textLabel.text = dataModel.name;
  11. cell.detailTextLabel.text = dataModel.detail;
  12.  
  13. return cell;

4、接下来我们来设置section,我们用一个自定义的UIView类型的对象来作为section的header,也就是表头<th>。

4.1、在UITableViewController 的(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section方法中指定view。

  1. HeaderView *header = [HeaderView headerView:tableView];//这里的HeaderView是我们自己定义的一个继承UITableViewHeaderFooterView的类
  2. header.delegate = self; //设置header对象delegate为当前控制器
  3. header.groupModel = self.dataArray[section];//设置header对象groupModel成员为某个二级数组
  4. return header;//返回UIView *

4.2、接着我们定义HeaderView这个类

HeaderView.h

  1. @protocol HeaderViewDelegate <NSObject>
  2.  
  3. @optional
  4. - (void)clickView; //首先定义一个代理方法,让TableViewController取实现,点击section的header时候要显示cell
  5.  
  6. @end
  7.  
  8. @interface HeaderView : UITableViewHeaderFooterView
  9. @property (nonatomic, assign) id<HeaderViewDelegate> delegate;//有protocol的代理,肯定要有这个代理了
  10.  
  11. @property (nonatomic, strong) JKGroupModel *groupModel;//单个section的group数据
  12. + (instancetype)headerView:(UITableView *)tableView;//声明类的getter方法,这里说明一下instancetype,这个数据类型在这个类里就是HeaderView *,也就是self的数据类型,但这个instancetype只能做返回值,不能做参数类型。
  13. @end

HeaderView.m

  1. @implementation HeaderView{//我们先定义一些全局变量,全局变量也能这样写?
  2. UIButton *_arrowBtn;
  3. UILabel *_label;
  4. }
  5.  
  6. //重写initWithReuseIdentifier
  7. - (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
  8. if (self = [super init]) {//先让父类自己搞他自己的那一套初始化
  9. UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//我们定义一个UIButton,在UIButton上面自己实现怎么显示数据
  10. [button setBackgroundImage:[UIImage imageNamed:@"header_bg"] forState:UIControlStateNormal];
  11. [button setBackgroundImage:[UIImage imageNamed:@"header_bg_highlighted"] forState:UIControlStateHighlighted];
  12. [button setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
  13. [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  14. button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  15. button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
  16. button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
  17. button.imageView.contentMode = UIViewContentModeCenter;
  18. [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];//点击section的header时,应该显示cell或者收起cell
  19. button.imageView.clipsToBounds = NO;
  20. _arrowBtn = button;
  21. [self addSubview:_arrowBtn];//添加到视图
  22. //创建label,显示当前在线人数
  23. UILabel *labelRight = [[UILabel alloc] init];
  24. labelRight.textAlignment = NSTextAlignmentCenter;
  25. _label = labelRight;
  26. [self addSubview:_label];//添加到视图
  27. }
  28. return self;
  29. }
  30.  
  31. //点击section的header时,应该显示cell或者收起cell
  32. - (void)buttonAction{
  33. self.groupModel.isOpen = !self.groupModel.isOpen;
  34. if ([self.delegate respondsToSelector:@selector(clickView)]) {//respondsToSelector这个方法是查找clickView是否存在
  35. [self.delegate clickView];//delegate的方法clickView的实现在TableViewController里
  36. }
  37. }
  38.  
  39. //下面是一下布局和图像的东西didMoveToSuperview,layoutSubviews,setGroupModel,这里先不讲
  40. - (void)didMoveToSuperview{
  41. _arrowBtn.imageView.transform = self.groupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) :CGAffineTransformMakeRotation(0);
  42. }
  43. //布局
  44. - (void)layoutSubviews{
  45. [super layoutSubviews];
  46. _arrowBtn.frame = self.bounds;
  47. _label.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
  48. }
  49. //赋值
  50. - (void)setGroupModel:(JKGroupModel *)groupModel{
  51. _groupModel = groupModel;
  52. [_arrowBtn setTitle:_groupModel.name forState:UIControlStateNormal];
  53. _label.text = [NSString stringWithFormat:@"%@/%lu",_groupModel.online,(unsigned long)_groupModel.friends.count];
  54. }

5、这里我们实现一下HeaderView中声明的一个代理clickView

  1. - (void)clickView{
  2. [self.tableView reloadData];//这个reloadData会重新执行所有的UITableView显示类方法,相当刷新这个UITableView
  3. }

6、这里我们实现点击 cell的时候push到一个视图,这里没有太多说明了

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2. ViewController *viewCtrl = [[ViewController alloc] init];
  3. [self.navigationController pushViewController:viewCtrl animated:NO];
  4. }

7、这里我们实现用一个TableFooterView盖住多余的section线。

  1. - (void)clipExtraCellLine:(UITableView *)tableView{
  2. UIView *view = [[UIView alloc] init];
  3. view.backgroundColor = [UIColor clearColor];
  4. [self.tableView setTableFooterView:view];
  5. }

IOS初级:UITableView的更多相关文章

  1. iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)

    iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...

  2. Unity3D for iOS初级教程:Part 3/3

    转自Unity 3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这份教程是由教程团队成员 ...

  3. iOS programming UITableView and UITableViewController

    iOS programming  UITableView and UITableViewController A UITableView displays a single column of dat ...

  4. Unity3D for iOS初级教程:Part 3/3(上)

    转自:http://www.cnblogs.com/alongu3d/archive/2013/06/01/3111738.html 欢迎来到第三部分,这是Unity 3D for iOS初级系列教程 ...

  5. iOS之UITableView组头组尾视图/标题悬停

    最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...

  6. iOS 编辑UITableView(根据iOS编程编写)

    上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为  e ...

  7. 【IOS】从android角度来实现(理解)IOS的UITableView

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3403124.html   本人从在学校开始到现在上班(13年毕 ...

  8. iOS 关于UITableView的dequeueReusableCellWithIdentifier

    今天看代码的时候,突然想到了以前的一个问题. 刚学ios那会儿,常会写出以下代码 - (UITableViewCell *)tableView:(UITableView *)tableView cel ...

  9. iOS开发 UITableView之cell

    1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...

随机推荐

  1. 第七篇:Jmeter连接MySQL的测试

    .准备一个有数据表格的MySQL数据库: 2.在测试计划面板上点击浏览按钮,把你的JDBC驱动添加进来: mysql-connector-java-5.1.26-bin.jar 3.添加一个线程组-- ...

  2. 大数据入门到精通4--spark的rdd的map使用方式

    学习了之前的rdd的filter以后,这次来讲spark的map方式 1.获得文件 val collegesRdd= sc.textFile("/user/hdfs/CollegeNavig ...

  3. 使用tor网络

    在www.torproject.org/projects/torbrowser.html.en上找到合适的版本下载 下载好tor浏览器之后,解压双击Tor Browser,出现这个错误 这是因为kal ...

  4. Session和Cookie的理解

    原文地址:https://juejin.im/post/5aede266f265da0ba266e0ef

  5. Django项目的创建与管理和pycharm与Github的秘密

    随笔 - 174  文章 - 21  评论 - 19 Django项目创建与管理   1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...

  6. 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密

    后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...

  7. 项目总结10:通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题

    通过反射解决springboot环境下从redis取缓存进行转换时出现ClassCastException异常问题 关键字 springboot热部署  ClassCastException异常 反射 ...

  8. swift - 画图截取图片 - 保存相册

    1.图片截取 func test(addView:UIView) -> UIImage?{ UIGraphicsBeginImageContextWithOptions(CGSize(width ...

  9. linux下Redis主从复制

    Redis的主从配置比起MySQL主从配置简单多了,而且Redis主从复制中一个主服务可以有多个从服务,一个从服务又可以有多个从服务. MySQL主从配置http://www.cnblogs.com/ ...

  10. JFinal Web开发学习(一)开启HelloWorld

    初次接触JFinal框架,接触过MVC思想,接触过Spring. JFinal官网: http://www.jfinal.com/ 之前很嫌弃JavaWeb开发,主要原因是繁琐的xml配置. 官方推荐 ...