CCTableView 简单样例
非常像android中的listview
#pragma once; #include "cocos2d.h"
using namespace cocos2d; //使用CCTableView必须包括扩展库和命名空间
#include "cocos-ext.h"
USING_NS_CC_EXT; //须要实现CCTabelViewDelegate和CCTabelViewDataSource这俩个接口
class tableTest : public cocos2d::CCLayer,public CCTableViewDelegate,public CCTableViewDataSource
{
public:
virtual bool init(); static cocos2d::CCScene* scene(); CREATE_FUNC(tableTest); //继承自以上的接口须要实现的方法例如以下
//从CCTableViewDataSource继承下来的 CCSize tableCellSizeForIndex(CCTableView * table,unsigned int index);
CCTableViewCell * tableCellAtIndex(CCTableView * table,unsigned int index);
unsigned int numberOfCellsInTableView(CCTableView * table);
virtual CCSize cellSizeForTable (CCTableView *table); //下面俩个函数能够覆写,也能够不覆写,是从CCTableViewDelegate继承下来的
void tableCellHighlight(CCTableView * table,CCTableViewCell * cell);
void tableCellUnhighlight(CCTableView * table,CCTableViewCell * cell);
void tableCellTouched(CCTableView * table,CCTableViewCell * cell);
virtual void tableCellWillRecycle (CCTableView *table, CCTableViewCell *cell);
//由于继承自CCScrollViewDelegate 所以要实现这俩个方法,里边一般都不写东西
void scrollViewDidScroll(CCScrollView *){};
void scrollViewDidZoom(CCScrollView *){};
};
#include "tableTest.h"
USING_NS_CC_EXT;
USING_NS_CC;
bool tableTest::init()
{
if ( !CCLayer::init() )
{
return false;
} CCSize size = CCDirector::sharedDirector()->getWinSize(); /*
创建一个竖直方向的tableview
*/ //初始化的时候第一个參数是CCTableViewDataSource。第二个參数代表tableview的大小
CCTableView * table = CCTableView::create(this,CCSize(100,320)); //设置delegate代理
table->setDelegate(this); //设置tableview的滑动的方向
//kCCScrollViewDirectionHorizontal 水平
//kCCScrollViewDirectionVertical 竖直
table->setDirection(kCCScrollViewDirectionVertical); //CCTableView默认是以左下角点设置坐标位置的,它继承自CCLayer,这一点不难理解
table->setPosition(ccp(size.width/5,0));
table->setDataSource(this);
this->addChild(table); /*
创建一个水平方向的tableview
*/
CCTableView * tableView = CCTableView::create(this,CCSize(size.width/2,50));
tableView->setDelegate(this);
tableView->setDirection( kCCScrollViewDirectionHorizontal);
tableView->setPosition(ccp(size.width/2,size.height/2));
table->setDataSource(this);
this->addChild(tableView);
//这句话一定要加上啊,意思是用现有的配置去刷新全部的cell方法被调用之后。系统会又一次运行一遍TableViewDelegate的相关函数。最基本的cellFor***方法,单元格的设置信息会被又一次运行一遍。 tableView->reloadData(); //相当android中adapte.notificydatachange(); return true;
} //这个函数是用来获得cell的
CCTableViewCell * tableTest::tableCellAtIndex(CCTableView * table,unsigned int index)
{
CCString * string = CCString::createWithFormat("%d",index+1); //获得一个可用的cell,由于在我们滑动cell的时候有些cell是显示的。有些不是显示出来的。把没有渲染的cell拿过来
//这样就不用又一次new出一个cell了,这种话能够减小内存的开销
CCTableViewCell * cell = table->dequeueCell();
if(!cell)
{
cell = new CCTableViewCell();
cell->autorelease(); //加入背景图片到cell中,便于区分边界
CCSprite * background = CCSprite::create("cell.png");
background->setAnchorPoint(ccp(0,0));
background->setPosition(CCPointZero);
cell->addChild(background,0); //加入文本信息到cell中
CCLabelTTF * text = CCLabelTTF::create(string->getCString(),"",20);
text->setPosition(ccp(25,25));
text->setTag(1);
text->setColor(ccc3(255,0,0));
cell->addChild(text,1); //加入精灵到cell的中心位置
CCSprite * sprite = CCSprite::create("icon.png");
sprite->setPosition(ccp(50,25));
cell->addChild(sprite,1);
}
//else中获得是没有渲染出来的cell,cell中原有的内容还存在
else
{
//改变原来cell中的文本信息
CCLabelTTF * text = (CCLabelTTF *)cell->getChildByTag(1);
text->setString(string->getCString());
} return cell;
} //这里设置每一个cell的大小
CCSize tableTest::tableCellSizeForIndex(CCTableView * table,unsigned int index)
{
if(index=3){
return CCSize(300,50);}
else{
return CCSize(100,50);
}
} //这里设置一共同拥有多少个cell
unsigned int tableTest::numberOfCellsInTableView(CCTableView * table)
{
return 20;
}
//和tableCellSizeForIndex有什么差别不明确
cocos2d::CCSize tableTest::cellSizeForTable(CCTableView *table)
{
return CCSize(200,50);
} //假设某个cell被点击了,则会调用此函数
void tableTest::tableCellHighlight(CCTableView * table,CCTableViewCell * cell)
{
CCLog("%d:tableCellHighlight!",cell->getIdx()+1);
} //点击之后会调用这个函数,观察这几个函数的调用顺序。发现highlight首先调用
//unhighlight然后调用,最后是tableCellTouched
void tableTest::tableCellUnhighlight(CCTableView * table,CCTableViewCell * cell)
{
CCLog("%d:tableCellUnhighlight!",cell->getIdx()+1);
}
//这里设置cell被点击以后的回调函数
void tableTest::tableCellTouched(CCTableView * table,CCTableViewCell * cell)
{
CCLog("%d:tableCellTouched!",cell->getIdx()+1);
}
void tableTest::tableCellWillRecycle(CCTableView *table, CCTableViewCell *cell)
{
CCLog("%d:tableCellWillRecycle!",cell->getIdx()+1);
}
cocos2d::CCScene* tableTest::scene()
{
CCScene* scene=CCScene::create();
scene->addChild(tableTest::create());
return scene;
}
CCTableView 简单样例的更多相关文章
- extern外部方法使用C#简单样例
外部方法使用C#简单样例 1.添加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- velocity简单样例
velocity简单样例整体实现须要三个步骤,详细例如以下: 1.创建一个Javaproject 2.导入须要的jar包 3.创建须要的文件 ============================= ...
- 自己定义隐式转换和显式转换c#简单样例
自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行 ...
- VC6 鼠标钩子 最简单样例
Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...
- gtk+3.0的环境配置及基于gtk+3.0的python简单样例
/********************************************************************* * Author : Samson * Date ...
- java 使用tess4j实现OCR的最简单样例
网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...
- 使用SALT-API进入集成开发的简单样例
测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...
- VB.net数据库编程(03):一个SQLserver连接查询的简单样例
这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System. ...
随机推荐
- mysql函数操作(3)
<?php $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); $dbh->s ...
- <meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Pragma" content="no-cache"><meta http-equiv="Cac ...
- javascript - 工作笔记 (事件绑定)
背景: 目前所做的项目,只能使用的是原生的javascript.对于javascript的事件绑定想必大家都懂得语法: 1,在标签中使用属性调用方法:<div onclick="AAA ...
- 练习一下linux中的list函数。
所有的list函数见 include/linux/list.h 自己从 include/linux/list.h 拷贝了一些函数到自己的list.c中, 然后练习了一下. 没有别的目的,就是想熟练一下 ...
- Python文件或目录操作的常用函数
◆ os.listdir(path) Return a list containing the names of the entries in the directory given by path. ...
- 从Excel转Access的一个方法说开去(DataRow的state状态)
因为客户对access不太熟悉,更喜欢玩EXCEL.但是系统要求导入ACCESS.所以我们得做个把EXCEL转换成Access的小工具.(别问我为啥不让系统直接导入excel....我不知道!),然后 ...
- PDF417码制尺寸定义
PDF417码制尺寸定义 1.模块宽度(X)不得小于0.191mm,你们目前所用300dpi打印机点的尺寸是0.084 mm * 0.01 mm,可 三倍以上值 2.条 ...
- STL模板_概念
模板和STL一.模板的背景知识1.针对不同的类型定义不同函数版本.2.借助参数宏摆脱类型的限制,同时也因为失去的类型检查而引 入风险.3.借助于编译预处理器根据函数宏框架,扩展为针对不同类型的 具体函 ...
- 0. chromium源代码分析 - 序
本打算在CSDN写完这系列文字,却因为在CSDN中误删了一篇blog,该篇blog被移到了回收站.然而CSDN居然没有从回收站撤销删除的操作方法.联想到之前CSDN泄密的问题,其可靠性值得怀疑.随转向 ...
- IOS-将长文字转化成图片方法
我们在看微博时,会看到一些长图片上的显示文章,现在就介绍下如何实现.分析下还是很简单的,总结如下:1.计算文字区域的高 2.利用UIGraphics图形上下文方法来实现 3.验证方法:UIImageW ...