CCMenuItemToggle multi-choice Implementation of CCTableView

Source: Internet
Author: User

[Cpp]
// TableViewAddToggle. h

// TableViewAddToggle. h
[Cpp]
# Ifndef _ TABLEVIEW_TOGGLE_H __
# Define _ TABLEVIEW_TOGGLE_H __
 
# Pragma once
# Include "common. h"
# Define maxnum180
 
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 maxnum180

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


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.