老师手把手教了两天半,看了一下模式,加了几个功能就大功告成了!!!
给我的感想就是全都是指针!
添加图片精灵:
CCSprite* spBG = CCSprite::create("");
this->addChild(spBG); //添加到当前层
spGB->setPosition(ccp(x,y)); //设置坐标
spGB->setScale(0.5); //缩放
文字按钮:
CCMenuItemFont* pItem = CCMenuItemFont::create("wenzi",this,menu_selector(回调函数));
再将文字按钮添加到菜单中
CCMenu* menu = CCMen::create(pItem,NULL);
this->addChild(menu);
menu->setPosition(ccp(x,y));
//创建文本
CCLabelTTF* ttf = CCLabelTTF::create("wenben","字体",48.0f);
this->addChild(ttf);
ttf->setPosition(cco(x,y));
//创建输入框(1、九点图,2、输入框)
CCScale9Sprite* input = CCScale9Sprite::create("");
accountBox = CCEditBox::create(CCSizeMake(r,c),input);
this->addChild(accounBox);
accountBox->setPosition(ccp(x,y));
accountBox->setInputFlag(kEditBoxInputFlagPassword); //输入模式为密码输入
按钮回调函数,看输入框accountBox的内容是否是密码;
不是密码:
弹出输入框 CCMessageBox("","");
是密码切换场景
CCDirector* director = CCDirector::sharedDirector();
director->replaceScene(ListViewLayer::scene()); //另一个头文件,里面的一个场景;
接下来,和之前的一样了;无非就是一些按钮,文本,输入框。
在init()里面画好面板;
各种按钮里面的回调函数。
然后中文转码utf-8老师已经写好了;
贴上自己写的东西,其他的老师已经写好了
#ifndef _Students_H_
#define _Students_H_
#include <iostream>
using namespace std;
class Students
{
public:
int m_iNumber;
char m_strName[];
char m_sex;
public:
Students(int iNumber, char* strName,char sex);
};
#endif
Students.h
#include "Students.h" Students::Students(int iNumber, char* strName,char sex)
{
m_iNumber = iNumber;
strcpy(m_strName, strName);
m_sex = sex;
}
Students.cpp
#ifndef _Manager_H_
#define _Manager_H_
#include <iostream>
using namespace std;
#include <vector>
#include "Students.h"
class Manager
{
private:
Manager();
public:
static Manager* m_pStance;
static Manager* getInstance(); // 管理学生容器
vector<Students*> _vStudent;
}; #endif
Manager.h
#include "Manager.h" Manager* Manager::m_pStance = NULL;
Manager* Manager::getInstance()
{
if(m_pStance == NULL)
{
m_pStance = new Manager;
}
return m_pStance;
} Manager::Manager()
{
Students* s = new Students(,"TreeDream",'F');
_vStudent.push_back(s);
s = new Students(,"马里奥", 'M');
_vStudent.push_back(s);
}
Manager.cpp
#ifndef __TABLEVIEWTESTSCENE_H__
#define __TABLEVIEWTESTSCENE_H__ #include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT; #include "ChineseString.h"
#include "Manager.h" //需要实现CCTabelViewDelegate和CCTabelViewDataSource这两个接口
class ListViewLayer : public CCLayer,
public CCTableViewDataSource,
public CCTableViewDelegate
{
private:
CCTableView* pTableView;
vector<Students*>::iterator it; CCLabelTTF* ttf; //提示信息 // 添加对话框需要的UI
CCLayerColor* layerBg;
CCEditBox* edit_id;
CCEditBox* edit_name;
CCEditBox* edit_sex;
CCMenu* menu_add_done;
// 添加按钮回调函数
void AddCallBack(CCObject* obj);
// 添加按钮回调函数
void AddDoneCallBack(CCObject* obj);
// 添加按钮回调函数
void AddDel(CCObject* obj);
void DelDoneCallBack(CCObject* obj); void Oride(CCObject* obj);
void OrideCallBack(CCObject* obj);
void OrideInf(CCObject* obj); void Searchs(CCObject* obj);
void searchsInf(CCObject* obj); void MySort (); public:
static cocos2d::CCScene* scene();
// 初始化层
virtual bool init(); /*因为CCTableView继承自CCScrollView所以要实现这俩个方法,里边一般都不写东西*/
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view); /*CCTabelViewDelegate类的虚函数*/
//处理触摸点击事件
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell); /*CCTabelViewDataSource类的虚函数*/
//每一项的宽度和高度
virtual CCSize cellSizeForTable(CCTableView *table);
//生成列表每一项的内容
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);
//一共多少项
virtual unsigned int numberOfCellsInTableView(CCTableView *table); // 创建层
CREATE_FUNC(ListViewLayer);
}; #endif
ListViewLayer.h
#include "ListViewLayer.h" CCScene* ListViewLayer::scene()
{
CCScene *scene = CCScene::create();
ListViewLayer *layer = ListViewLayer::create();
scene->addChild(layer);
return scene;
} bool ListViewLayer::init()
{
// 添加背景
CCSprite* spBg = CCSprite::create("HelloWorld.jpg");
this->addChild(spBg);
spBg->setPosition(ccp(/, /));
// 放大
spBg->setScale(1.5f); bool bRet = false;
do
{
CC_BREAK_IF( !CCLayer::init() ); // 创建对话框(参数:窗口大小)
pTableView = CCTableView::create(this, CCSizeMake(, ));
// 设置表格方向(垂直)
pTableView->setDirection(kCCScrollViewDirectionVertical);
// 设置坐标
pTableView->setPosition(CCPointZero);
// 设置delegate代理
pTableView->setDelegate(this);
// 设置起点(从上往下)
pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
// 添加到当前层
this->addChild(pTableView);
// 必须加,意思是用现有的配置去刷新所有的cell
pTableView->reloadData(); bRet = true;
}while(); // 添加按钮
CCMenuItemImage* item_add = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(ListViewLayer::AddCallBack));
// 添加菜单 CCMenuItemImage* item_del = CCMenuItemImage::create(
"del.png",
"CloseSelected.png",
this,
menu_selector(ListViewLayer::AddDel)); CCMenuItemImage*item_oride = CCMenuItemImage::create(
"oride.png",
"CloseSelected.png",
this,
menu_selector(ListViewLayer::Oride)); CCMenuItemImage* item_search = CCMenuItemImage::create(
"search.png",
"search.png",
this,
menu_selector(ListViewLayer::Searchs)); CCMenu* menu_add = CCMenu::create(item_add,item_del,item_oride,item_search,NULL);
item_del->setPositionY();
item_oride->setPositionY();
item_search->setPositionY();
this->addChild(menu_add);
menu_add->setPosition(ccp(,)); return true;
} void ListViewLayer::Searchs(CCObject* obj) { // 添加半透明的背景
layerBg = CCLayerColor::create(ccc4(,,,));
this->addChild(layerBg); // 1. 创建点9图
CCScale9Sprite* input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_id = CCEditBox::create(
CCSizeMake(,),
input);
// 3.添加当前层
this->addChild(edit_id);
// 4.设置坐标
edit_id->setPosition(ccp(,));
edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:")); // 完成添加菜单项
CCMenuItemFont* item_add_done = CCMenuItemFont::create(
ChineseString::GBKToUTF8("查询"),
this,
menu_selector(ListViewLayer::searchsInf));
menu_add_done = CCMenu::create(item_add_done,NULL);
this->addChild(menu_add_done);
menu_add_done->setPosition(ccp(,)); } void ListViewLayer::searchsInf(CCObject* obj) { char* name = (char*)ChineseString::UTF8ToGBK(edit_id->getText()); this->removeChild(ttf);
this->removeChild(edit_id);
this->removeChild(layerBg);
this->removeChild(menu_add_done); bool flag = false;
Students* sTemp;
for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) {
sTemp = *it;
if(strcmp(name,sTemp->m_strName)==) {
// 显示详细内容
ttf = CCLabelTTF::create(
ChineseString::GBKToUTF8(CCString::createWithFormat
("%d %s %c",
sTemp->m_iNumber,
sTemp->m_strName,
sTemp->m_sex)->getCString()),
"Arial",
);
this->addChild(ttf);
ttf->setPosition(ccp(,));
flag = true;
break;
}
}
if(flag==false) {
// 显示详细内容
this->removeChild(ttf);
ttf = CCLabelTTF::create(
ChineseString::GBKToUTF8(CCString::createWithFormat
("%s %s",name,"不存在")->getCString()),
"Arial",
);
this->addChild(ttf);
ttf->setPosition(ccp(,));
}
} //产生学号检查
void ListViewLayer::Oride(CCObject* obj) { // 添加半透明的背景
layerBg = CCLayerColor::create(ccc4(,,,));
this->addChild(layerBg); // 1. 创建点9图
CCScale9Sprite* input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_id = CCEditBox::create(
CCSizeMake(,),
input);
// 3.添加当前层
this->addChild(edit_id);
// 4.设置坐标
edit_id->setPosition(ccp(,));
edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); // 完成添加菜单项
CCMenuItemFont* item_add_done = CCMenuItemFont::create(
ChineseString::GBKToUTF8("修改"),
this,
menu_selector(ListViewLayer::OrideCallBack));
menu_add_done = CCMenu::create(item_add_done,NULL);
this->addChild(menu_add_done);
menu_add_done->setPosition(ccp(,)); } //产生更替信息
void ListViewLayer::OrideCallBack(CCObject* obj) { this->removeChild(edit_id);
this->removeChild(menu_add_done);
this->removeChild(layerBg); bool flag = false; int id = ChineseString::StringToInt(edit_id->getText());
for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) {
Students* sTemp = *it;
if(id==sTemp->m_iNumber) {
flag = true;
break;
}
}
if(flag==true) {
// 添加半透明的背景
layerBg = CCLayerColor::create(ccc4(,,,));
this->addChild(layerBg); // 1. 创建点9图
CCScale9Sprite* input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_id
= CCEditBox::create(
CCSizeMake(,),
input);
// 3.添加当前层
this->addChild(edit_id);
// 4.设置坐标
edit_id->setPosition(ccp(,));
edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_name = CCEditBox::create(
CCSizeMake(,),
input);
edit_name->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:"));
// 3.添加当前层
this->addChild(edit_name);
// 4.设置坐标
edit_name->setPosition(ccp(,)); input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_sex = CCEditBox::create(
CCSizeMake(,),
input);
edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("请输入性别:"));
// 3.添加当前层
this->addChild(edit_sex);
// 4.设置坐标
edit_sex->setPosition(ccp(,)); // 完成添加菜单项
CCMenuItemFont* item_add_done = CCMenuItemFont::create(
ChineseString::GBKToUTF8("修改"),
this,
menu_selector(ListViewLayer::OrideInf));
menu_add_done = CCMenu::create(item_add_done,NULL);
this->addChild(menu_add_done);
menu_add_done->setPosition(ccp(,));
}
else {
// 显示详细内容 ttf = CCLabelTTF::create(
ChineseString::GBKToUTF8(CCString::createWithFormat
("%d %s",id,"这个ID不存在")->getCString()),
"Arial",
);
this->addChild(ttf);
ttf->setPosition(ccp(,));
}
} void ListViewLayer::OrideInf(CCObject* obj) { int id2 = ChineseString::StringToInt(edit_id->getText());
char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText());
char sex = 'M';
Students* s = new Students(id2,name,sex);
*it = s; this->removeChild(ttf);
this->removeChild(edit_id);
this->removeChild(edit_name);
this->removeChild(edit_sex);
this->removeChild(menu_add_done);
this->removeChild(layerBg); MySort();
pTableView->reloadData();
} // 添加按钮回调函数
void ListViewLayer::AddCallBack(CCObject* obj)
{
// 添加半透明的背景
layerBg = CCLayerColor::create(ccc4(,,,));
this->addChild(layerBg); // 1. 创建点9图
CCScale9Sprite* input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_id = CCEditBox::create(
CCSizeMake(,),
input);
// 3.添加当前层
this->addChild(edit_id);
// 4.设置坐标
edit_id->setPosition(ccp(,));
edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_name = CCEditBox::create(
CCSizeMake(,),
input);
edit_name->setPlaceHolder(ChineseString::GBKToUTF8("请输入姓名:"));
// 3.添加当前层
this->addChild(edit_name);
// 4.设置坐标
edit_name->setPosition(ccp(,)); input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_sex = CCEditBox::create(
CCSizeMake(,),
input);
edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("请输入性别:"));
// 3.添加当前层
this->addChild(edit_sex);
// 4.设置坐标
edit_sex->setPosition(ccp(,)); // 完成添加菜单项
CCMenuItemFont* item_add_done = CCMenuItemFont::create(
ChineseString::GBKToUTF8("添加"),
this,
menu_selector(ListViewLayer::AddDoneCallBack));
menu_add_done = CCMenu::create(item_add_done,NULL);
this->addChild(menu_add_done);
menu_add_done->setPosition(ccp(,));
} // 添加按钮回调函数(字符添加)
void ListViewLayer::AddDoneCallBack(CCObject* obj)
{
this->removeChild(ttf);
// 处理添加信息 int id = ChineseString::StringToInt(edit_id->getText());
char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText()); char sex = edit_sex->getText()[];
Students* s = new Students(id,name,sex);
Manager::getInstance()->_vStudent.push_back(s);
// 关闭对话框
this->removeChild(edit_id);
this->removeChild(edit_name);
this->removeChild(edit_sex);
this->removeChild(menu_add_done);
this->removeChild(layerBg);
// 刷新列表 MySort(); pTableView->reloadData();
} void ListViewLayer::AddDel(CCObject* obj) {
// 添加半透明的背景
layerBg = CCLayerColor::create(ccc4(,,,));
this->addChild(layerBg); // 1. 创建点9图
CCScale9Sprite* input = CCScale9Sprite::create("input.png");
// 2. 创建输入框
edit_id = CCEditBox::create(
CCSizeMake(,),
input);
// 3.添加当前层
this->addChild(edit_id);
// 4.设置坐标
edit_id->setPosition(ccp(,));
edit_id->setPlaceHolder(ChineseString::GBKToUTF8("请输入学号:")); // 完成添加菜单项
CCMenuItemFont* item_add_done = CCMenuItemFont::create(
ChineseString::GBKToUTF8("删除"),
this,
menu_selector(ListViewLayer::DelDoneCallBack));
menu_add_done = CCMenu::create(item_add_done,NULL);
this->addChild(menu_add_done);
menu_add_done->setPosition(ccp(,)); } void ListViewLayer::DelDoneCallBack(CCObject* obj) {
int id = ChineseString::StringToInt(edit_id->getText()); bool flag = false; vector<Students*>::iterator it;
for(it=Manager::getInstance()->_vStudent.begin();
it!=Manager::getInstance()->_vStudent.end();it++) {
Students* sTemp = *it;
if (id==sTemp->m_iNumber)
{
// 删除元素
Manager::getInstance()->_vStudent.erase(it);
flag = true;
break;
}
} if(flag==false) {
this->removeChild(ttf);
ttf = CCLabelTTF::create(
ChineseString::GBKToUTF8(CCString::createWithFormat
("%d %s",id,"这个ID不存在")->getCString()),
"Arial",
);
this->addChild(ttf);
ttf->setPosition(ccp(,));
} //关闭
this->removeChild(edit_id);
this->removeChild(menu_add_done);
this->removeChild(layerBg); MySort();
pTableView->reloadData(); } bool cmpById(Students* a,Students *b) {
if(a->m_iNumber<b->m_iNumber)
return true;
return false;
} void ListViewLayer::MySort () {
std::sort(Manager::getInstance()->_vStudent.begin(),Manager::getInstance()->_vStudent.end(),cmpById);
} // cell被点击后的回调函数
void ListViewLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
pTableView->reloadData();
} //这里设置每个cell的大小
CCSize ListViewLayer::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(, );
} CCTableViewCell* ListViewLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
/*
获得一个可用的cell,因为在我们滑动cell的时候有些cell是显示的,
有些不是显示出来的,把没有渲染的cell拿过来,这样就不用重新new出一个cell了,
可以减小内存的开销
*/
Students* sTmep = Manager::getInstance()->_vStudent[idx];
CCString *pString = CCString::createWithFormat(
"%d %s %c", sTmep->m_iNumber ,
sTmep->m_strName, sTmep->m_sex);
CCTableViewCell *pCell = table->dequeueCell();
if (!pCell) {
pCell = new CCTableViewCell();
pCell->autorelease();
// 单个选项背景
CCSprite *pSprite = CCSprite::create("listitem.png");
pSprite->setAnchorPoint(CCPointZero);
pSprite->setPosition(CCPointZero);
pCell->addChild(pSprite);
pSprite->setScale(0.5f); CCLabelTTF *pLabel = CCLabelTTF::create(ChineseString::GBKToUTF8(pString->getCString()), "楷体", 20.0);
pLabel->setColor(ccc3(, , ));
pLabel->setFontSize();
pLabel->setPosition(ccp(,));
pLabel->setAnchorPoint(CCPointZero);
pLabel->setTag();
pCell->addChild(pLabel);
}
else//获得是没有渲染出来的cell,cell中原有的内容还存在
{
CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag();
pLabel->setString(ChineseString::GBKToUTF8(pString->getCString()));
}
return pCell;
} // 这里设置一共有多少个cell
unsigned int ListViewLayer::numberOfCellsInTableView(CCTableView *table)
{
// 返回容器的大小
return Manager::getInstance()->_vStudent.size();
} void ListViewLayer::scrollViewDidScroll(CCScrollView *view)
{
} void ListViewLayer::scrollViewDidZoom(CCScrollView *view)
{
}
ListViewLayer.cpp