學生信息管理系統(cocos2d引擎)——數據結構課程設計

老師手把手教了兩天半,看了一下模式,加了幾個功能就大功告成了!!!ios

給個人感想就是全都是指針!ide

 

添加圖片精靈:函數

CCSprite*  spBG = CCSprite::create("");字體

this->addChild(spBG);  //添加到當前層this

spGB->setPosition(ccp(x,y));  //設置座標spa

spGB->setScale(0.5);  //縮放代理

 

文字按鈕:指針

CCMenuItemFont* pItem = CCMenuItemFont::create("wenzi",this,menu_selector(回調函數));code

再將文字按鈕添加到菜單中orm

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));

//建立輸入框(一、九點圖,二、輸入框)

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老師已經寫好了;

貼上本身寫的東西,其餘的老師已經寫好了

 

 1 #ifndef _Students_H_
 2 #define _Students_H_
 3 #include <iostream>
 4 using namespace std;
 5 class Students
 6 {
 7 public:
 8     int m_iNumber;
 9     char m_strName[32];
10     char m_sex;
11 public:
12     Students(int iNumber, char* strName,char sex);
13 };
14 #endif
Students.h
1 #include "Students.h"
2 
3 
4 Students::Students(int iNumber, char* strName,char sex)
5 {
6     m_iNumber = iNumber;
7     strcpy(m_strName, strName);
8     m_sex = sex;
9 }
Students.cpp
 1 #ifndef _Manager_H_
 2 #define _Manager_H_
 3 #include <iostream>
 4 using namespace std;
 5 #include <vector>
 6 #include "Students.h"
 7 class Manager
 8 {
 9 private:
10     Manager();
11 public:
12     static Manager* m_pStance;
13     static Manager* getInstance();
14 
15     // 管理學生容器
16     vector<Students*> _vStudent;
17 };
18 
19 #endif
Manager.h
 1 #include "Manager.h"
 2 
 3 Manager* Manager::m_pStance = NULL;
 4 Manager* Manager::getInstance()
 5 {
 6     if(m_pStance == NULL)
 7     {
 8         m_pStance = new Manager;
 9     }
10     return m_pStance;
11 }
12 
13 Manager::Manager()
14 {
15     Students* s = new Students(1000,"TreeDream",'F');
16     _vStudent.push_back(s);
17     s = new Students(1001,"馬里奧", 'M');
18     _vStudent.push_back(s);
19 }
Manager.cpp
 1 #ifndef __TABLEVIEWTESTSCENE_H__
 2 #define __TABLEVIEWTESTSCENE_H__
 3 
 4 #include "cocos2d.h"
 5 #include "cocos-ext.h"
 6 USING_NS_CC;
 7 USING_NS_CC_EXT;
 8 
 9 #include "ChineseString.h"
10 #include "Manager.h"
11 
12 //須要實現CCTabelViewDelegate和CCTabelViewDataSource這兩個接口
13 class ListViewLayer : public CCLayer, 
14     public CCTableViewDataSource, 
15     public CCTableViewDelegate
16 {
17 private:
18     CCTableView* pTableView;
19     vector<Students*>::iterator it;
20 
21     CCLabelTTF* ttf;    //提示信息
22 
23     // 添加對話框須要的UI
24     CCLayerColor* layerBg;
25     CCEditBox* edit_id;
26     CCEditBox* edit_name;
27     CCEditBox* edit_sex;
28     CCMenu* menu_add_done;
29     //  添加按鈕回調函數
30     void AddCallBack(CCObject* obj);
31     //  添加按鈕回調函數
32     void AddDoneCallBack(CCObject* obj);
33     // 添加按鈕回調函數
34     void AddDel(CCObject* obj);
35     void DelDoneCallBack(CCObject* obj);
36 
37     void Oride(CCObject* obj);
38     void OrideCallBack(CCObject* obj);
39     void OrideInf(CCObject* obj);
40 
41 
42     void Searchs(CCObject* obj);
43     void searchsInf(CCObject* obj);
44 
45     void MySort ();
46 
47 public:
48     static cocos2d::CCScene* scene();
49     // 初始化層
50     virtual bool init();
51 
52     /*由於CCTableView繼承自CCScrollView因此要實現這倆個方法,裏邊通常都不寫東西*/
53     virtual void scrollViewDidScroll(CCScrollView* view);
54     virtual void scrollViewDidZoom(CCScrollView* view);
55 
56     /*CCTabelViewDelegate類的虛函數*/
57     //處理觸摸點擊事件
58     virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell);
59 
60     /*CCTabelViewDataSource類的虛函數*/
61     //每一項的寬度和高度
62     virtual CCSize cellSizeForTable(CCTableView *table);
63     //生成列表每一項的內容
64     virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);
65     //一共多少項
66     virtual unsigned int numberOfCellsInTableView(CCTableView *table);
67 
68     // 建立層
69     CREATE_FUNC(ListViewLayer);
70 };
71 
72 #endif 
ListViewLayer.h
  1 #include "ListViewLayer.h"
  2 
  3 CCScene* ListViewLayer::scene()
  4 {
  5     CCScene *scene = CCScene::create();
  6     ListViewLayer *layer = ListViewLayer::create();
  7     scene->addChild(layer);
  8     return scene;
  9 }
 10 
 11 bool ListViewLayer::init()
 12 {
 13     //  添加背景
 14     CCSprite* spBg = CCSprite::create("HelloWorld.jpg");
 15     this->addChild(spBg);
 16     spBg->setPosition(ccp(1240/2, 640/2));
 17     // 放大
 18     spBg->setScale(1.5f);
 19 
 20     bool bRet = false;
 21     do
 22     {
 23         CC_BREAK_IF( !CCLayer::init() );
 24 
 25         // 建立對話框(參數:窗口大小)
 26         pTableView = CCTableView::create(this, CCSizeMake(960, 600));
 27         // 設置表格方向(垂直)
 28         pTableView->setDirection(kCCScrollViewDirectionVertical);
 29         // 設置座標
 30         pTableView->setPosition(CCPointZero);
 31         // 設置delegate代理
 32         pTableView->setDelegate(this);
 33         // 設置起點(從上往下)
 34         pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
 35         // 添加到當前層
 36         this->addChild(pTableView);
 37         // 必須加,意思是用現有的配置去刷新全部的cell
 38         pTableView->reloadData();
 39 
 40 
 41         bRet = true;
 42     }while(0);
 43 
 44     // 添加按鈕
 45     CCMenuItemImage* item_add = CCMenuItemImage::create(
 46         "CloseNormal.png",
 47         "CloseSelected.png",
 48         this,
 49         menu_selector(ListViewLayer::AddCallBack));
 50     // 添加菜單
 51 
 52     CCMenuItemImage* item_del = CCMenuItemImage::create(
 53         "del.png",
 54         "CloseSelected.png",
 55         this,
 56         menu_selector(ListViewLayer::AddDel));
 57 
 58     CCMenuItemImage*item_oride = CCMenuItemImage::create(
 59         "oride.png",
 60         "CloseSelected.png",
 61         this,
 62         menu_selector(ListViewLayer::Oride));
 63 
 64     CCMenuItemImage* item_search = CCMenuItemImage::create(
 65         "search.png",
 66         "search.png",
 67         this,
 68         menu_selector(ListViewLayer::Searchs));
 69 
 70     CCMenu* menu_add = CCMenu::create(item_add,item_del,item_oride,item_search,NULL);
 71     item_del->setPositionY(50);
 72     item_oride->setPositionY(100);
 73     item_search->setPositionY(150);
 74     this->addChild(menu_add);
 75     menu_add->setPosition(ccp(700,300));
 76 
 77     return true;
 78 }
 79 
 80 void ListViewLayer::Searchs(CCObject* obj) {
 81 
 82     // 添加半透明的背景
 83     layerBg = CCLayerColor::create(ccc4(0,0,0,127));
 84     this->addChild(layerBg);
 85 
 86     // 1. 建立點9圖
 87     CCScale9Sprite* input = CCScale9Sprite::create("input.png");
 88     // 2. 建立輸入框
 89     edit_id = CCEditBox::create(
 90         CCSizeMake(220,40),
 91         input);
 92     // 3.添加當前層
 93     this->addChild(edit_id);
 94     // 4.設置座標
 95     edit_id->setPosition(ccp(450,300));
 96     edit_id->setPlaceHolder(ChineseString::GBKToUTF8("請輸入姓名:"));
 97 
 98     // 完成添加菜單項
 99     CCMenuItemFont* item_add_done = CCMenuItemFont::create(
100         ChineseString::GBKToUTF8("查詢"),
101         this,
102         menu_selector(ListViewLayer::searchsInf));
103     menu_add_done = CCMenu::create(item_add_done,NULL);
104     this->addChild(menu_add_done);
105     menu_add_done->setPosition(ccp(450,50));
106 
107 }
108 
109 void ListViewLayer::searchsInf(CCObject* obj) {
110 
111 
112     char* name = (char*)ChineseString::UTF8ToGBK(edit_id->getText());
113 
114     this->removeChild(ttf);
115     this->removeChild(edit_id);
116     this->removeChild(layerBg);
117     this->removeChild(menu_add_done);
118     
119     bool flag = false;
120     Students* sTemp;
121     for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) {
122         sTemp = *it;
123         if(strcmp(name,sTemp->m_strName)==0) {
124             // 顯示詳細內容
125             ttf = CCLabelTTF::create(
126                 ChineseString::GBKToUTF8(CCString::createWithFormat
127                 ("%d %s %c",
128                 sTemp->m_iNumber,
129                 sTemp->m_strName,
130                 sTemp->m_sex)->getCString()),
131                 "Arial",
132                 24);
133             this->addChild(ttf);
134             ttf->setPosition(ccp(100,100));
135             flag = true;
136             break;
137         }
138     }
139     if(flag==false) {
140         // 顯示詳細內容
141         this->removeChild(ttf);
142         ttf = CCLabelTTF::create(
143             ChineseString::GBKToUTF8(CCString::createWithFormat
144             ("%s %s",name,"不存在")->getCString()),
145             "Arial",
146             24);
147         this->addChild(ttf);
148         ttf->setPosition(ccp(100,100));
149     }
150 }
151 
152 //產生學號檢查
153 void ListViewLayer::Oride(CCObject* obj) {
154 
155     // 添加半透明的背景
156     layerBg = CCLayerColor::create(ccc4(0,0,0,127));
157     this->addChild(layerBg);
158 
159     // 1. 建立點9圖
160     CCScale9Sprite* input = CCScale9Sprite::create("input.png");
161     // 2. 建立輸入框
162     edit_id = CCEditBox::create(
163         CCSizeMake(220,40),
164         input);
165     // 3.添加當前層
166     this->addChild(edit_id);
167     // 4.設置座標
168     edit_id->setPosition(ccp(450,300));
169     edit_id->setPlaceHolder(ChineseString::GBKToUTF8("請輸入學號:"));
170 
171     // 完成添加菜單項
172     CCMenuItemFont* item_add_done = CCMenuItemFont::create(
173         ChineseString::GBKToUTF8("修改"),
174         this,
175         menu_selector(ListViewLayer::OrideCallBack));
176     menu_add_done = CCMenu::create(item_add_done,NULL);
177     this->addChild(menu_add_done);
178     menu_add_done->setPosition(ccp(450,50));
179 
180 }
181 
182 //產生更替信息
183 void ListViewLayer::OrideCallBack(CCObject* obj) {
184 
185     this->removeChild(edit_id);
186     this->removeChild(menu_add_done);
187     this->removeChild(layerBg);
188     
189 
190     bool flag = false;
191 
192     int id = ChineseString::StringToInt(edit_id->getText());
193     for(it = Manager::getInstance()->_vStudent.begin();it!=Manager::getInstance()->_vStudent.end();it++) {
194         Students* sTemp = *it;
195         if(id==sTemp->m_iNumber) {
196             flag = true;
197             break;
198         }
199     }
200     if(flag==true) {
201         // 添加半透明的背景
202         layerBg = CCLayerColor::create(ccc4(0,0,0,127));
203         this->addChild(layerBg);
204 
205 
206         // 1. 建立點9圖
207         CCScale9Sprite* input = CCScale9Sprite::create("input.png");
208         // 2. 建立輸入框
209         edit_id
210             = CCEditBox::create(
211             CCSizeMake(220,40),
212             input);
213         // 3.添加當前層
214         this->addChild(edit_id);
215         // 4.設置座標
216         edit_id->setPosition(ccp(450,300));
217         edit_id->setPlaceHolder(ChineseString::GBKToUTF8("請輸入學號:"));
218 
219         input = CCScale9Sprite::create("input.png");
220         // 2. 建立輸入框
221         edit_name = CCEditBox::create(
222             CCSizeMake(220,40),
223             input);
224         edit_name->setPlaceHolder(ChineseString::GBKToUTF8("請輸入姓名:"));
225         // 3.添加當前層
226         this->addChild(edit_name);
227         // 4.設置座標
228         edit_name->setPosition(ccp(450,200));
229 
230         input = CCScale9Sprite::create("input.png");
231         // 2. 建立輸入框
232         edit_sex = CCEditBox::create(
233             CCSizeMake(220,40),
234             input);
235         edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("請輸入性別:"));
236         // 3.添加當前層
237         this->addChild(edit_sex);
238         // 4.設置座標
239         edit_sex->setPosition(ccp(450,100));
240 
241         // 完成添加菜單項
242         CCMenuItemFont* item_add_done = CCMenuItemFont::create(
243             ChineseString::GBKToUTF8("修改"),
244             this,
245             menu_selector(ListViewLayer::OrideInf));
246         menu_add_done = CCMenu::create(item_add_done,NULL);
247         this->addChild(menu_add_done);
248         menu_add_done->setPosition(ccp(450,50));
249     }
250     else {
251         // 顯示詳細內容
252         
253         ttf = CCLabelTTF::create(
254             ChineseString::GBKToUTF8(CCString::createWithFormat
255             ("%d %s",id,"這個ID不存在")->getCString()),
256             "Arial",
257             24);
258         this->addChild(ttf);
259         ttf->setPosition(ccp(100,100));
260     }
261 }
262 
263 void ListViewLayer::OrideInf(CCObject* obj) {
264     
265     int id2 = ChineseString::StringToInt(edit_id->getText());
266     char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText());
267     char sex = 'M';
268     Students* s = new Students(id2,name,sex);
269     *it = s;
270 
271     this->removeChild(ttf);
272     this->removeChild(edit_id);
273     this->removeChild(edit_name);
274     this->removeChild(edit_sex);
275     this->removeChild(menu_add_done);
276     this->removeChild(layerBg);
277 
278     MySort();
279     pTableView->reloadData();
280 }
281 
282 //  添加按鈕回調函數
283 void ListViewLayer::AddCallBack(CCObject* obj)
284 {
285     // 添加半透明的背景
286     layerBg = CCLayerColor::create(ccc4(0,0,0,127));
287     this->addChild(layerBg);
288 
289 
290     // 1. 建立點9圖
291     CCScale9Sprite* input = CCScale9Sprite::create("input.png");
292     // 2. 建立輸入框
293     edit_id = CCEditBox::create(
294         CCSizeMake(220,40),
295         input);
296     // 3.添加當前層
297     this->addChild(edit_id);
298     // 4.設置座標
299     edit_id->setPosition(ccp(450,300));
300     edit_id->setPlaceHolder(ChineseString::GBKToUTF8("請輸入學號:"));
301 
302     input = CCScale9Sprite::create("input.png");
303     // 2. 建立輸入框
304     edit_name = CCEditBox::create(
305         CCSizeMake(220,40),
306         input);
307     edit_name->setPlaceHolder(ChineseString::GBKToUTF8("請輸入姓名:"));
308     // 3.添加當前層
309     this->addChild(edit_name);
310     // 4.設置座標
311     edit_name->setPosition(ccp(450,200));
312 
313     input = CCScale9Sprite::create("input.png");
314     // 2. 建立輸入框
315     edit_sex = CCEditBox::create(
316         CCSizeMake(220,40),
317         input);
318     edit_sex->setPlaceHolder(ChineseString::GBKToUTF8("請輸入性別:"));
319     // 3.添加當前層
320     this->addChild(edit_sex);
321     // 4.設置座標
322     edit_sex->setPosition(ccp(450,100));
323 
324     // 完成添加菜單項
325     CCMenuItemFont* item_add_done = CCMenuItemFont::create(
326         ChineseString::GBKToUTF8("添加"),
327         this,
328         menu_selector(ListViewLayer::AddDoneCallBack));
329     menu_add_done = CCMenu::create(item_add_done,NULL);
330     this->addChild(menu_add_done);
331     menu_add_done->setPosition(ccp(450,50));
332 }
333 
334 //  添加按鈕回調函數(字符添加)
335 void ListViewLayer::AddDoneCallBack(CCObject* obj)
336 {
337     this->removeChild(ttf);
338     //  處理添加信息
339 
340     int id = ChineseString::StringToInt(edit_id->getText());
341     char* name = (char*)ChineseString::UTF8ToGBK(edit_name->getText());
342     
343     char sex = edit_sex->getText()[0];
344     Students* s = new Students(id,name,sex);
345     Manager::getInstance()->_vStudent.push_back(s);
346     // 關閉對話框
347     this->removeChild(edit_id);
348     this->removeChild(edit_name);
349     this->removeChild(edit_sex);
350     this->removeChild(menu_add_done);
351     this->removeChild(layerBg);
352     // 刷新列表
353 
354     MySort();
355 
356     pTableView->reloadData();
357 }
358 
359 
360 void ListViewLayer::AddDel(CCObject* obj) {
361     // 添加半透明的背景
362     layerBg = CCLayerColor::create(ccc4(0,0,0,127));
363     this->addChild(layerBg);
364 
365     // 1. 建立點9圖
366     CCScale9Sprite* input = CCScale9Sprite::create("input.png");
367     // 2. 建立輸入框
368     edit_id = CCEditBox::create(
369         CCSizeMake(220,40),
370         input);
371     // 3.添加當前層
372     this->addChild(edit_id);
373     // 4.設置座標
374     edit_id->setPosition(ccp(450,300));
375     edit_id->setPlaceHolder(ChineseString::GBKToUTF8("請輸入學號:"));
376     
377     // 完成添加菜單項
378     CCMenuItemFont* item_add_done = CCMenuItemFont::create(
379         ChineseString::GBKToUTF8("刪除"),
380         this,
381         menu_selector(ListViewLayer::DelDoneCallBack));
382     menu_add_done = CCMenu::create(item_add_done,NULL);
383     this->addChild(menu_add_done);
384     menu_add_done->setPosition(ccp(450,50));
385 
386 }
387 
388 void ListViewLayer::DelDoneCallBack(CCObject* obj) {
389     int id = ChineseString::StringToInt(edit_id->getText());
390     
391     bool flag = false;
392 
393     vector<Students*>::iterator it;
394     for(it=Manager::getInstance()->_vStudent.begin();
395         it!=Manager::getInstance()->_vStudent.end();it++) {
396             Students* sTemp = *it;    
397             if (id==sTemp->m_iNumber)
398             {
399                 // 刪除元素
400                 Manager::getInstance()->_vStudent.erase(it);
401                 flag = true;
402                 break;
403             }
404     }
405 
406     if(flag==false) {
407         this->removeChild(ttf);
408         ttf = CCLabelTTF::create(
409             ChineseString::GBKToUTF8(CCString::createWithFormat
410             ("%d %s",id,"這個ID不存在")->getCString()),
411             "Arial",
412             24);
413         this->addChild(ttf);
414         ttf->setPosition(ccp(100,100));
415     }
416 
417     //關閉
418     this->removeChild(edit_id);
419     this->removeChild(menu_add_done);
420     this->removeChild(layerBg);
421 
422     MySort();
423     pTableView->reloadData();
424 
425 }
426 
427 bool cmpById(Students* a,Students *b) {
428     if(a->m_iNumber<b->m_iNumber)
429         return true;
430     return false;
431 }
432 
433 void ListViewLayer::MySort () {
434     std::sort(Manager::getInstance()->_vStudent.begin(),Manager::getInstance()->_vStudent.end(),cmpById);
435 }
436 
437 
438 // cell被點擊後的回調函數
439 void ListViewLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
440 {
441     pTableView->reloadData();
442 }
443 
444 //這裏設置每一個cell的大小
445 CCSize ListViewLayer::cellSizeForTable(CCTableView *table)
446 {
447     return CCSizeMake(600, 60);
448 }
449 
450 CCTableViewCell* ListViewLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
451 {
452     /* 
453     得到一個可用的cell,由於在咱們滑動cell的時候有些cell是顯示的,
454     有些不是顯示出來的,把沒有渲染的cell拿過來,這樣就不用從新new出一個cell了,
455     能夠減少內存的開銷
456     */
457     Students* sTmep = Manager::getInstance()->_vStudent[idx];
458     CCString *pString = CCString::createWithFormat(
459         "%d %s %c", sTmep->m_iNumber , 
460         sTmep->m_strName, sTmep->m_sex);
461     CCTableViewCell *pCell = table->dequeueCell();
462     if (!pCell) {
463         pCell = new CCTableViewCell();
464         pCell->autorelease();
465         // 單個選項背景
466         CCSprite *pSprite = CCSprite::create("listitem.png");
467         pSprite->setAnchorPoint(CCPointZero);
468         pSprite->setPosition(CCPointZero);
469         pCell->addChild(pSprite);
470         pSprite->setScale(0.5f);
471 
472         CCLabelTTF *pLabel = CCLabelTTF::create(ChineseString::GBKToUTF8(pString->getCString()), "楷體", 20.0);
473         pLabel->setColor(ccc3(255, 255, 0));
474         pLabel->setFontSize(48);
475         pLabel->setPosition(ccp(20,20));
476         pLabel->setAnchorPoint(CCPointZero);
477         pLabel->setTag(123);
478         pCell->addChild(pLabel);
479     }
480     else//得到是沒有渲染出來的cell,cell中原有的內容還存在
481     {
482         CCLabelTTF *pLabel = (CCLabelTTF*)pCell->getChildByTag(123);
483         pLabel->setString(ChineseString::GBKToUTF8(pString->getCString()));
484     }
485     return pCell;
486 }
487 
488 // 這裏設置一共有多少個cell
489 unsigned int ListViewLayer::numberOfCellsInTableView(CCTableView *table)
490 {
491     // 返回容器的大小
492     return Manager::getInstance()->_vStudent.size();
493 }
494 
495 void ListViewLayer::scrollViewDidScroll(CCScrollView *view)
496 {
497 }
498 
499 void ListViewLayer::scrollViewDidZoom(CCScrollView *view)
500 {
501 }
ListViewLayer.cpp
相關文章
相關標籤/搜索