IOS初级:UITableView
先来看一下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中的数据。
static NSString *identifier = @"dataCell";//这个用于cell种类的标识
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; //按identifier查找可重用cell。也就是系统要是有identifier类型的cell给你用,就返回这个可用的cell给你,否则返回nil
if (!cell) {//如果没有
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];//老子新建一个 }
DataGroupModel *groupModel = self.dataArray[indexPath.section]; //这里的dataArray可以是一个NSMutableArray,包含二级数据,第一级group用于section,第二级data用于cell
DataModel *dataModel = groupModel.data[indexPath.row]; //定为到group的data级数据
cell.imageView.image = [UIImage imageNamed:dataModel.icon];//这里cell的imageView等成员是UITableViewCell自己带的
cell.textLabel.text = dataModel.name;
cell.detailTextLabel.text = dataModel.detail; return cell;
4、接下来我们来设置section,我们用一个自定义的UIView类型的对象来作为section的header,也就是表头<th>。
4.1、在UITableViewController 的(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section方法中指定view。
HeaderView *header = [HeaderView headerView:tableView];//这里的HeaderView是我们自己定义的一个继承UITableViewHeaderFooterView的类
header.delegate = self; //设置header对象delegate为当前控制器
header.groupModel = self.dataArray[section];//设置header对象groupModel成员为某个二级数组
return header;//返回UIView *
4.2、接着我们定义HeaderView这个类
HeaderView.h
@protocol HeaderViewDelegate <NSObject> @optional
- (void)clickView; //首先定义一个代理方法,让TableViewController取实现,点击section的header时候要显示cell @end @interface HeaderView : UITableViewHeaderFooterView
@property (nonatomic, assign) id<HeaderViewDelegate> delegate;//有protocol的代理,肯定要有这个代理了 @property (nonatomic, strong) JKGroupModel *groupModel;//单个section的group数据
+ (instancetype)headerView:(UITableView *)tableView;//声明类的getter方法,这里说明一下instancetype,这个数据类型在这个类里就是HeaderView *,也就是self的数据类型,但这个instancetype只能做返回值,不能做参数类型。
@end
HeaderView.m
@implementation HeaderView{//我们先定义一些全局变量,全局变量也能这样写?
UIButton *_arrowBtn;
UILabel *_label;
}
//重写initWithReuseIdentifier
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super init]) {//先让父类自己搞他自己的那一套初始化
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//我们定义一个UIButton,在UIButton上面自己实现怎么显示数据
[button setBackgroundImage:[UIImage imageNamed:@"header_bg"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"header_bg_highlighted"] forState:UIControlStateHighlighted];
[button setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
button.imageView.contentMode = UIViewContentModeCenter;
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];//点击section的header时,应该显示cell或者收起cell
button.imageView.clipsToBounds = NO;
_arrowBtn = button;
[self addSubview:_arrowBtn];//添加到视图
//创建label,显示当前在线人数
UILabel *labelRight = [[UILabel alloc] init];
labelRight.textAlignment = NSTextAlignmentCenter;
_label = labelRight;
[self addSubview:_label];//添加到视图
}
return self;
}
//点击section的header时,应该显示cell或者收起cell
- (void)buttonAction{
self.groupModel.isOpen = !self.groupModel.isOpen;
if ([self.delegate respondsToSelector:@selector(clickView)]) {//respondsToSelector这个方法是查找clickView是否存在
[self.delegate clickView];//delegate的方法clickView的实现在TableViewController里
}
}
//下面是一下布局和图像的东西didMoveToSuperview,layoutSubviews,setGroupModel,这里先不讲
- (void)didMoveToSuperview{
_arrowBtn.imageView.transform = self.groupModel.isOpen ? CGAffineTransformMakeRotation(M_PI_2) :CGAffineTransformMakeRotation(0);
}
//布局
- (void)layoutSubviews{
[super layoutSubviews];
_arrowBtn.frame = self.bounds;
_label.frame = CGRectMake(self.frame.size.width - 70, 0, 60, self.frame.size.height);
}
//赋值
- (void)setGroupModel:(JKGroupModel *)groupModel{
_groupModel = groupModel;
[_arrowBtn setTitle:_groupModel.name forState:UIControlStateNormal];
_label.text = [NSString stringWithFormat:@"%@/%lu",_groupModel.online,(unsigned long)_groupModel.friends.count];
}
5、这里我们实现一下HeaderView中声明的一个代理clickView
- (void)clickView{
[self.tableView reloadData];//这个reloadData会重新执行所有的UITableView显示类方法,相当刷新这个UITableView
}
6、这里我们实现点击 cell的时候push到一个视图,这里没有太多说明了
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
ViewController *viewCtrl = [[ViewController alloc] init];
[self.navigationController pushViewController:viewCtrl animated:NO];
}
7、这里我们实现用一个TableFooterView盖住多余的section线。
- (void)clipExtraCellLine:(UITableView *)tableView{
UIView *view = [[UIView alloc] init];
view.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:view];
}
IOS初级:UITableView的更多相关文章
- iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath(汇总)
iOS中UITableView的cell点击事件不触发didSelectRowAtIndexPath 首先分析有几种原因,以及相应的解决方法 1.UITableViewCell的userInterac ...
- Unity3D for iOS初级教程:Part 3/3
转自Unity 3D for iOS 这篇文章还可以在这里找到 英语 Learn how to use Unity to make a simple 3D iOS game! 这份教程是由教程团队成员 ...
- iOS programming UITableView and UITableViewController
iOS programming UITableView and UITableViewController A UITableView displays a single column of dat ...
- Unity3D for iOS初级教程:Part 3/3(上)
转自:http://www.cnblogs.com/alongu3d/archive/2013/06/01/3111738.html 欢迎来到第三部分,这是Unity 3D for iOS初级系列教程 ...
- iOS之UITableView组头组尾视图/标题悬停
最近笔者在公司的iOS开发中,有一个iOS开发同事跑来问了两个问题:1.给UITableView设置了组头和组尾视图,但是一直显示不出来?2.UITableView的section的header和fo ...
- iOS 编辑UITableView(根据iOS编程编写)
上个项目我们完成了 JXHomepwner 简单的应用展示,项目地址.本节我们需要在上节项目基础上,增加一些响应用户操作.包括添加,删除和移动表格. 编辑模式 UITableView 有一个名为 e ...
- 【IOS】从android角度来实现(理解)IOS的UITableView
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3403124.html 本人从在学校开始到现在上班(13年毕 ...
- iOS 关于UITableView的dequeueReusableCellWithIdentifier
今天看代码的时候,突然想到了以前的一个问题. 刚学ios那会儿,常会写出以下代码 - (UITableViewCell *)tableView:(UITableView *)tableView cel ...
- iOS开发 UITableView之cell
1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...
随机推荐
- 单元测试框架unittest
单元测试:单元测试,是指对软件中的最小可测试单元进行检查和验证,对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义如:c语言中单元指一个函数,java里单元指一个类,图形化的软件中可以 ...
- php 更新array键值
$arr1 = array("loginname" => "username","psw" => "password& ...
- app和wap手机网站的区别在哪里
第一点 我们从依附的平台来看: 移动Wap网站:由移动设备的浏览器来支持,只要移动设备支持上网浏览网站基本上可以随时随地的打开网站查找自己需要的信息. 移动App客户端:由智能移动设备的操作系统来支持 ...
- Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题
Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...
- SRVE0255E: 尚未定义要处理 ***的 Web 组/虚拟主机。
原因: 访问的端口未设置,因此无法访问 解决办法: 1.设置端口 控制台-环境-虚拟主机-default_host-主机别名-新建 主机名根据实际情况填写 笔者这里填如下 2.设置好端口后重启对应服务 ...
- c++面向行的输入getline()和get()
来源:c++ primer plus 在c++里当我们输入一个字符串时习惯用cin,但是cin只能读取一段不含空格的字符串,如果我们需要读取一段包含空格的字符串时,就需要用到getline()或get ...
- 再谈AR中的图像识别算法
之前在<浅谈移动平台创新玩法>简单的猜测了easyar中使用的图像识别算法,基于图片指纹的哈希算法的图片检索 .后再阿里引商大神的指点下,意识到图片检测只适用于静态图片的识别,只能做AR脱 ...
- manipulate
manipulate - 必应词典 美[mə'nɪpjə.leɪt]英[mə'nɪpjʊleɪt] v.控制:摆布:(有技巧地)使用:巧妙地处理(问题等) 网络操纵:被操纵:被控体 变形第三人称单数: ...
- [leetcode]445. Add Two Numbers II 两数相加II
You are given two non-empty linked lists representing two non-negative integers. The most significan ...
- Wechat微信公众平台开发
一.微信概述 1.历史背景 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册用户量突破 ...