UITableView的UITableViewStyleGrouped

以下图例就是分组UITableViewStyleGrouped的样式

本人提供快速集成的方法,不弄脏你那双手:)

源码:

TableViewData.h

  1. //
  2. // TableVewData.h
  3. // Sections
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import <Foundation/Foundation.h>
  9.  
  10. @interface TableViewData : NSObject
  11.  
  12. // 添加数据源 + 数据源标签
  13. - (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag;
  14.  
  15. // 对应区域中的row的个数
  16. - (NSInteger)numberOfRowsInSection:(NSInteger)section;
  17.  
  18. // 有几个section
  19. - (NSInteger)numberOfSections;
  20.  
  21. // 对应于Section上的flag值标签
  22. - (NSString *)flagInSection:(NSInteger)section;
  23.  
  24. // 对应于indexPath中的数据
  25. - (id)dataInIndexPath:(NSIndexPath *)indexPath;
  26.  
  27. @end

TableViewData.m

  1. //
  2. // TableVewData.m
  3. // Sections
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "TableViewData.h"
  9.  
  10. @interface TableViewData ()
  11.  
  12. @property (nonatomic, strong) NSMutableArray *dataArray;
  13. @property (nonatomic, strong) NSMutableArray *nameList;
  14.  
  15. @end
  16.  
  17. @implementation TableViewData
  18.  
  19. - (instancetype)init
  20. {
  21. self = [super init];
  22. if (self)
  23. {
  24. _dataArray = [NSMutableArray new];
  25. _nameList = [NSMutableArray new];
  26. }
  27. return self;
  28. }
  29.  
  30. - (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag
  31. {
  32. [_dataArray addObject:array];
  33. [_nameList addObject:flag];
  34. }
  35.  
  36. - (NSInteger)numberOfRowsInSection:(NSInteger)section
  37. {
  38. return [_dataArray[section] count];
  39. }
  40.  
  41. - (NSInteger)numberOfSections
  42. {
  43. return [_dataArray count];
  44. }
  45.  
  46. - (NSString *)flagInSection:(NSInteger)section
  47. {
  48. return _nameList[section];
  49. }
  50.  
  51. - (id)dataInIndexPath:(NSIndexPath *)indexPath
  52. {
  53. return _dataArray[indexPath.section][indexPath.row];
  54. }
  55.  
  56. @end

使用情况:

  1. //
  2. // RootViewController.m
  3. // UITableViewGroup
  4. //
  5. // Copyright (c) 2014年 Y.X. All rights reserved.
  6. //
  7.  
  8. #import "RootViewController.h"
  9. #import "TableViewData.h"
  10.  
  11. static NSString *reusedFlag = @"reusedFlag";
  12.  
  13. @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
  14.  
  15. @property (nonatomic, strong) UITableView *tableView;
  16. @property (nonatomic, strong) TableViewData *tableViewData;
  17.  
  18. @end
  19.  
  20. @implementation RootViewController
  21.  
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25.  
  26. // 初始化tableView(分组形势的tableView)
  27. _tableView = [[UITableView alloc] initWithFrame:self.view.bounds
  28. style:UITableViewStyleGrouped];
  29. _tableView.delegate = self;
  30. _tableView.dataSource = self;
  31. [self.view addSubview:_tableView];
  32.  
  33. // tableView数据
  34. _tableViewData = [TableViewData new];
  35. [_tableViewData addDataArray:@[@"", @"", @""] arrayFlag:@"设置"]; // section1
  36. [_tableViewData addDataArray:@[@"一", @"二", @"三"] arrayFlag:@"配置"]; // section2
  37. [_tableViewData addDataArray:@[@"one", @"two", @"three"] arrayFlag:@"闲置"]; // section3
  38. }
  39.  
  40. // 每个区多少个cell
  41. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  42. {
  43. return [_tableViewData numberOfRowsInSection:section];
  44. }
  45.  
  46. // 创建cell
  47. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  48. {
  49. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];
  50. if (cell == nil)
  51. {
  52. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
  53. reuseIdentifier:reusedFlag];
  54. }
  55.  
  56. // section1
  57. if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"设置"])
  58. {
  59. NSString *str = [_tableViewData dataInIndexPath:indexPath];
  60. cell.textLabel.text = str;
  61. cell.textLabel.textColor = [UIColor redColor];
  62. }
  63.  
  64. // section2
  65. if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"配置"])
  66. {
  67. NSString *str = [_tableViewData dataInIndexPath:indexPath];
  68. cell.textLabel.text = str;
  69. cell.textLabel.textColor = [UIColor blueColor];
  70. }
  71.  
  72. // section3
  73. if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"闲置"])
  74. {
  75. NSString *str = [_tableViewData dataInIndexPath:indexPath];
  76. cell.textLabel.text = str;
  77. cell.textLabel.textColor = [UIColor magentaColor];
  78. }
  79.  
  80. return cell;
  81. }
  82.  
  83. // 多少个区
  84. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  85. {
  86. return [_tableViewData numberOfSections];
  87. }
  88.  
  89. // 每个区的标题
  90. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  91. {
  92. return [_tableViewData flagInSection:section];
  93. }
  94.  
  95. @end

几个需要注意的地方:

以下两个方法是DataSource中的,直接与分组有关.

不同的section配置不同的标题.

看起来应该很直白:)

 

UITableView的UITableViewStyleGrouped的更多相关文章

  1. UItableView与UICollectionView

    UITableView 1. UITableViewStyleGrouped 分区表格样式创建表格 .separatorStyle = UITableViewCellSeparatorStyleSin ...

  2. iOS UITableView 与 UITableViewController

    很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...

  3. 使用UITableView的分组样式

    分组样式顾名思义是对TableView中的数据行进行分组处理,每个分组都有一个header和footer. TableView中header的英文文本是大写的,footer的英文文本是小写的.如下图浅 ...

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

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

  5. UI第十八节——UITableView

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,基本大部分应用都有UITableView.当然它的广泛使用自然离不开它强大的功能,今天就针对U ...

  6. iOS开发系列--UITableView全面解析

    --UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...

  7. UITableView UITableViewCell

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  8. (转) 如何让 UITableView 的 headerView跟随 cell一起滚动

    在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就 ...

  9. UItableVIew初探

    UItableView style/* //普通  UITableViewStylePlain,   //分组  UITableViewStyleGrouped*/ //表格视图    UITable ...

随机推荐

  1. 深度学习(七)U-Net原理以及keras代码实现医学图像眼球血管分割

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9780786.html DRIVE数据集下载百度云链接:链接:https://pan.baidu ...

  2. sqlplus 调试存储过程

    SQLPLUS里测试带返回参数的存储过程过程名p_test入参 aa varchar2出参 bb sys_refcursor 在SQLPLUS里如何将sys_refcursor 这个结果集获取出来呢 ...

  3. oracle 数据库添加Java方法

    create or replace and compile java source named "Bitconverter" aspublic class Bitconverter ...

  4. Web开发技术选型之Java与PHP

    PHP与J2EE的对比 网上有很多关于PHP与J2EE之间的对比,细观无非以下几点: 1.语言特征 PHP为脚本语言,解释型语言,弱类型,专为Web开发打造.Java为C语言系编程语言,编译型,强类型 ...

  5. Toolstrip 工具栏控件

    工具栏是另一种获取应用程序主要功能的常用方法,比起菜单更直观.   Tool strip 控件是由system.Windows.forms.Toolstrip类提供的,作用是创建易于自定义的常用工具栏 ...

  6. 【原】spring boot source 1.5 中不支持 diamond 运算符

    最近要开发新的项目,就花了几天时间看了下spring boot的相关资料,然后做了一个demo,不得不说开发效率确实很快,几行注解就完成了事务,aop,数据库等相关配置:但由于先前习惯了spring ...

  7. rgbdslam 源代码的实现

    经过一番努力,终于跑通了felix.endres的rgbd slam v2 源码,中间遇到挺多问题.总结如下: (1) 关于SiftGPU问题:ERROR: SiftGPU cannot be com ...

  8. Spring Boot 打包jar部署服务器

    部署方式:打包成jar部署 部署方式有两种,一种是传统的war包,另一种是打包成jar,推荐第二种方式部署 部署准备 1. jar包内置tomcat,无需服务器安装tomcat环境 2.需要JDK,且 ...

  9. linux下配置环境变量方式

    linux下配置环境变量有多种方式,下面简述之 方式1.编辑 /etc/profile 文件,增加如下内容 JAVA_HOME=/usr/local/jdk1. export JAVA_HOME PA ...

  10. java工厂模式个人体会

    上一边文章主要对单例模式做了一个总结,这篇文章主要对工厂模式也写一写个人的体会. 工厂模式是设计模式的一种,它主要是把实现产品对象的过程封装起来,然后提供给客户端相应的接口.工厂模式也是有3种,分别为 ...