UITableView的UITableViewStyleGrouped
UITableView的UITableViewStyleGrouped

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

本人提供快速集成的方法,不弄脏你那双手:)
源码:
TableViewData.h
//
// TableVewData.h
// Sections
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import <Foundation/Foundation.h> @interface TableViewData : NSObject // 添加数据源 + 数据源标签
- (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag; // 对应区域中的row的个数
- (NSInteger)numberOfRowsInSection:(NSInteger)section; // 有几个section
- (NSInteger)numberOfSections; // 对应于Section上的flag值标签
- (NSString *)flagInSection:(NSInteger)section; // 对应于indexPath中的数据
- (id)dataInIndexPath:(NSIndexPath *)indexPath; @end
TableViewData.m
//
// TableVewData.m
// Sections
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "TableViewData.h" @interface TableViewData () @property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *nameList; @end @implementation TableViewData - (instancetype)init
{
self = [super init];
if (self)
{
_dataArray = [NSMutableArray new];
_nameList = [NSMutableArray new];
}
return self;
} - (void)addDataArray:(NSArray *)array arrayFlag:(NSString *)flag
{
[_dataArray addObject:array];
[_nameList addObject:flag];
} - (NSInteger)numberOfRowsInSection:(NSInteger)section
{
return [_dataArray[section] count];
} - (NSInteger)numberOfSections
{
return [_dataArray count];
} - (NSString *)flagInSection:(NSInteger)section
{
return _nameList[section];
} - (id)dataInIndexPath:(NSIndexPath *)indexPath
{
return _dataArray[indexPath.section][indexPath.row];
} @end
使用情况:
//
// RootViewController.m
// UITableViewGroup
//
// Copyright (c) 2014年 Y.X. All rights reserved.
// #import "RootViewController.h"
#import "TableViewData.h" static NSString *reusedFlag = @"reusedFlag"; @interface RootViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) TableViewData *tableViewData; @end @implementation RootViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化tableView(分组形势的tableView)
_tableView = [[UITableView alloc] initWithFrame:self.view.bounds
style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[self.view addSubview:_tableView]; // tableView数据
_tableViewData = [TableViewData new];
[_tableViewData addDataArray:@[@"", @"", @""] arrayFlag:@"设置"]; // section1
[_tableViewData addDataArray:@[@"一", @"二", @"三"] arrayFlag:@"配置"]; // section2
[_tableViewData addDataArray:@[@"one", @"two", @"three"] arrayFlag:@"闲置"]; // section3
} // 每个区多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tableViewData numberOfRowsInSection:section];
} // 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedFlag];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:reusedFlag];
} // section1
if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"设置"])
{
NSString *str = [_tableViewData dataInIndexPath:indexPath];
cell.textLabel.text = str;
cell.textLabel.textColor = [UIColor redColor];
} // section2
if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"配置"])
{
NSString *str = [_tableViewData dataInIndexPath:indexPath];
cell.textLabel.text = str;
cell.textLabel.textColor = [UIColor blueColor];
} // section3
if ([[_tableViewData flagInSection:indexPath.section] isEqualToString:@"闲置"])
{
NSString *str = [_tableViewData dataInIndexPath:indexPath];
cell.textLabel.text = str;
cell.textLabel.textColor = [UIColor magentaColor];
} return cell;
} // 多少个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_tableViewData numberOfSections];
} // 每个区的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_tableViewData flagInSection:section];
} @end
几个需要注意的地方:
以下两个方法是DataSource中的,直接与分组有关.

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

看起来应该很直白:)

UITableView的UITableViewStyleGrouped的更多相关文章
- UItableView与UICollectionView
UITableView 1. UITableViewStyleGrouped 分区表格样式创建表格 .separatorStyle = UITableViewCellSeparatorStyleSin ...
- iOS UITableView 与 UITableViewController
很多应用都会在界面中使用某种列表控件:用户可以选中.删除或重新排列列表中的项目.这些控件其实都是UITableView 对象,可以用来显示一组对象,例如,用户地址薄中的一组人名.项目地址. UITab ...
- 使用UITableView的分组样式
分组样式顾名思义是对TableView中的数据行进行分组处理,每个分组都有一个header和footer. TableView中header的英文文本是大写的,footer的英文文本是小写的.如下图浅 ...
- iOS之UITableView组头组尾视图/标题悬停
最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...
- UI第十八节——UITableView
在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,基本大部分应用都有UITableView.当然它的广泛使用自然离不开它强大的功能,今天就针对U ...
- iOS开发系列--UITableView全面解析
--UIKit之UITableView 概述 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似于微信.QQ.新浪微博等软件基本上随处都是U ...
- UITableView UITableViewCell
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- (转) 如何让 UITableView 的 headerView跟随 cell一起滚动
在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就 ...
- UItableVIew初探
UItableView style/* //普通 UITableViewStylePlain, //分组 UITableViewStyleGrouped*/ //表格视图 UITable ...
随机推荐
- 【Qt开发】常用控件--QSpinBox和QDoubleSpinBox
QSpinBox和QDoubleSpinBox 是UI设计常用的控件. QSpinBox可用于显示和输入整数,并可以在显示框中添加前缀或后缀. QDoubleSpinBox可用于显示和输入小数,并可以 ...
- mysql去除重复记录案例
例1,表中有主键(可唯一标识的字段),且该字段为数字类型 1 测试数据 /* 表结构 */ DROP TABLE IF EXISTS `t1`; CREATE TABLE IF NOT EXISTS ...
- 基于node安装gulp-一些命令
1.win+r:进入cdm 2.node -v:查询是否安装node 3.npm install -g gulp:安装gulp 4.gulp -v:查看是否安装gulp 5.cd desk:进入桌面 ...
- 从java到web前端再到php,一路走来的小总结
java的学习: 初学者对Java的学习,上来的感觉都是比较难,感觉java的东西很多,如此多的类和接口.有时还弄不懂为啥实例化出一个int空数组为什么数组中默认都是0,实例化一个空字符串数组时(St ...
- DataGridview 绘制行序号
RowPostpaint 事件 通过Rectangle(矩形的意思)对象绘制矩形区域,然后在通过textRenderer的DeawText方法来绘制序列号. Rectangle(x,y width ...
- Vue 引入第三方js.css的方式
转自:https://blog.csdn.net/csdn_yudong/article/details/78795743 我们以 jQuery 为例,来讲解 一.绝对路径直接引入,全局可用 主入口页 ...
- ASP.NET Button控件的UseSubmitBehavior属性引发的血案
这里先不说标题上的UseSubmitBehavior属性是什么,先说下面这种情况. 通常,在我们写一个表单页面的时候,最下方会有“提交”和“返回”字样的两个按钮.顾名思义,它们的功能大家都知道,但是一 ...
- Hadoop学习笔记(10) ——搭建源码学习环境
Hadoop学习笔记(10) ——搭建源码学习环境 上一章中,我们对整个hadoop的目录及源码目录有了一个初步的了解,接下来计划深入学习一下这头神象作品了.但是看代码用什么,难不成gedit?,单步 ...
- NIO与Socket
一.Socket 的使用 1.单线程Socket的使用 /** * 单线程版本 * 问题描述:只能服务单个客户端 * 解决方案:多线程版本 */ public class Socket_V1 { pu ...
- sublime设置不提示更新
sublime 作为轻量级的编辑器非常好用,时不时提醒购买还好 但是经常还提醒更新就不能接受了 解决方法: Just go to Preferences -> Settings-User and ...