[cpp]
//TableViewAddToggle.h
//TableViewAddToggle.h
[cpp]
#ifndef __TABLEVIEW_TOGGLE_H__
#define __TABLEVIEW_TOGGLE_H__
#pragma once
#include "common.h"
#define MAXNUM 180
class CTableViewAddToggle :public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate
{
private:
CCTableViewCell *m_pCell;
CCTableView* m_pTableView;
CCMenu *m_pSelectMenu;
bool m_bIsSelect[MAXNUM];
CCMenu* m_pMenuSelect[MAXNUM];
CCMenuItemToggle* m_pToggleItem[MAXNUM];
public:
CTableViewAddToggle(void);
void onSelect(CCObject* pSender);
virtual void onEnter();
virtual void onExit();
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view){};
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view){};
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
CREATE_FUNC(CTableViewAddToggle);
};
#endif
#ifndef __TABLEVIEW_TOGGLE_H__
#define __TABLEVIEW_TOGGLE_H__
#pragma once
#include "common.h"
#define MAXNUM 180
class CTableViewAddToggle :public cocos2d::CCLayer,public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate
{
private:
CCTableViewCell *m_pCell;
CCTableView* m_pTableView;
CCMenu *m_pSelectMenu;
bool m_bIsSelect[MAXNUM];
CCMenu* m_pMenuSelect[MAXNUM];
CCMenuItemToggle* m_pToggleItem[MAXNUM];
public:
CTableViewAddToggle(void);
void onSelect(CCObject* pSender);
virtual void onEnter();
virtual void onExit();
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view){};
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view){};
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
CREATE_FUNC(CTableViewAddToggle);
};
#endif
[cpp]
//TableViewAddToggle
//TableViewAddToggle
[cpp]
?#include "CTableViewAddToggle .h"
#define BOXHEIGHT 100
#define BOXNUM 180
#define TABLE_SPACE 15
CTableViewAddToggle ::CTableViewAddToggle ()
{
m_bIsSelect[MAXNUM]=false;
}
void CTableViewAddToggle ::onEnter()
{
CCLayer::onEnter();
m_pTableView = CCTableView::create(this, CCSizeMake(SCEEN_WIDTH, (BOXHEIGHT*BOXNUM)+TABLE_SPACE*4));
m_pTableView->setDirection(kCCScrollViewDirectionVertical);
m_pTableView->setPosition(ccp( ORIGIN.x, WINSIZE.height/2 + ORIGIN.y-BOXHEIGHT*2.9f));
m_pTableView->setDelegate(this);
m_pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(m_pTableView);
m_pTableView->reloadData();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
}
void CTableViewAddToggle ::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
this->removeChild(m_pTableView);
}
void CTableViewAddToggle::onSelect(CCObject* pSender)
{
CCMenuItemToggle* temp[MAXLNUM];
temp[((CCMenuItemImage*)pSender)->getTag()]=(CCMenuItemToggle*)pSender;
if (temp[((CCMenuItemImage*)pSender)->getTag()]->getSelectedIndex()==0)
{
m_bIsSelect[((CCMenuItemImage*)pSender)->getTag()] = false;
}
if (temp[((CCMenuItemImage*)pSender)->getTag()]->getSelectedIndex()==1)
{
m_bIsSelect[((CCMenuItemImage*)pSender)->getTag()] = true;
}
}
void CTableViewAddToggle::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLog("cell touched at index: %i", cell->getIdx());
}
CCSize CTableViewAddToggle::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(SCEEN_WIDTH, BOXHEIGHT+TABLE_SPACE);
}
CCTableViewCell* CTableViewAddToggle::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
m_pCell=table->dequeueCell();
if (!m_pCell)
{
m_pCell = new CCTableViewCell();
m_pCell->autorelease();
}
m_pCell->removeAllChildrenWithCleanup(true);
CCString *pString = CCString::createWithFormat("%d", idx);
CCLabelTTF *pLabel = CCLabelTTF::create(pString->getCString(), "Arial", MISSION_TTFSIZE);
pLabel->setPosition(CCPointZero);
pLabel->setAnchorPoint(CCPointZero);
pLabel->setTag(BOXHEIGHT+TABLE_SPACE);
m_pCell->addChild(pLabel);
m_pToggleItem[idx]= CCMenuItemToggle::createWithTarget(this,
menu_selector(CGeneralTeach2::onSelect),
CCMenuItemImage::create("menu/select.png","menu/noselect.png"),
CCMenuItemImage::create("menu/noselect.png","menu/select.png"),
NULL );
m_pToggleItem[idx]->setTag(idx);
m_pMenuSelect[idx]=CCMenu::create(m_pToggleItem[idx], NULL);
m_pMenuSelect[idx]->setPosition(ccp(WINSIZE.width/2+m_pToggleItem[idx]->getContentSize().width*4.0f, BOXHEIGHT-TABLE_SPACE*3));
m_pCell->addChild(m_pMenuSelect[idx]);
if (m_bIsSelect[idx])
{
m_pToggleItem[idx]->setSelectedIndex(1);
}
else
{
m_pToggleItem[idx]->setSelectedIndex(0);
}
return m_pCell;
}
unsigned int CTableViewAddToggle::numberOfCellsInTableView(CCTableView *table)
{
return MAXNUM;
}
#include "CTableViewAddToggle .h"
#define BOXHEIGHT 100
#define BOXNUM 180
#define TABLE_SPACE 15
CTableViewAddToggle ::CTableViewAddToggle ()
{
m_bIsSelect[MAXNUM]=false;
}
void CTableViewAddToggle ::onEnter()
{
CCLayer::onEnter();
m_pTableView = CCTableView::create(this, CCSizeMake(SCEEN_WIDTH, (BOXHEIGHT*BOXNUM)+TABLE_SPACE*4));
m_pTableView->setDirection(kCCScrollViewDirectionVertical);
m_pTableView->setPosition(ccp( ORIGIN.x, WINSIZE.height/2 + ORIGIN.y-BOXHEIGHT*2.9f));
m_pTableView->setDelegate(this);
m_pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(m_pTableView);
m_pTableView->reloadData();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, false);
}
void CTableViewAddToggle ::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCLayer::onExit();
this->removeChild(m_pTableView);
}
void CTableViewAddToggle::onSelect(CCObject* pSender)
{
CCMenuItemToggle* temp[MAXLNUM];
temp[((CCMenuItemImage*)pSender)->getTag()]=(CCMenuItemToggle*)pSender;
if (temp[((CCMenuItemImage*)pSender)->getTag()]->getSelectedIndex()==0)
{
m_bIsSelect[((CCMenuItemImage*)pSender)->getTag()] = false;
}
if (temp[((CCMenuItemImage*)pSender)->getTag()]->getSelectedIndex()==1)
{
m_bIsSelect[((CCMenuItemImage*)pSender)->getTag()] = true;
}
}
void CTableViewAddToggle::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLog("cell touched at index: %i", cell->getIdx());
}
CCSize CTableViewAddToggle::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(SCEEN_WIDTH, BOXHEIGHT+TABLE_SPACE);
}
CCTableViewCell* CTableViewAddToggle::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
m_pCell=table->dequeueCell();
if (!m_pCell)
{
m_pCell = new CCTableViewCell();
m_pCell->autorelease();
}
m_pCell->removeAllChildrenWithCleanup(true);
CCString *pString = CCString::createWithFormat("%d", idx);
CCLabelTTF *pLabel = CCLabelTTF::create(pString->getCString(), "Arial", MISSION_TTFSIZE);
pLabel->setPosition(CCPointZero);
pLabel->setAnchorPoint(CCPointZero);
pLabel->setTag(BOXHEIGHT+TABLE_SPACE);
m_pCell->addChild(pLabel);
m_pToggleItem[idx]= CCMenuItemToggle::createWithTarget(this,
menu_selector(CGeneralTeach2::onSelect),
CCMenuItemImage::create("menu/select.png","menu/noselect.png"),
CCMenuItemImage::create("menu/noselect.png","menu/select.png"),
NULL );
m_pToggleItem[idx]->setTag(idx);
m_pMenuSelect[idx]=CCMenu::create(m_pToggleItem[idx], NULL);
m_pMenuSelect[idx]->setPosition(ccp(WINSIZE.width/2+m_pToggleItem[idx]->getContentSize().width*4.0f, BOXHEIGHT-TABLE_SPACE*3));
m_pCell->addChild(m_pMenuSelect[idx]);
if (m_bIsSelect[idx])
{
m_pToggleItem[idx]->setSelectedIndex(1);
}
else
{
m_pToggleItem[idx]->setSelectedIndex(0);
}
return m_pCell;
}
unsigned int CTableViewAddToggle::numberOfCellsInTableView(CCTableView *table)
{
return MAXNUM;
}