I wanted to write about CCTableView The article , But after the basic function is realized , The increasing demand of the project leads to a deeper research on this control .

well , Now let's start with this control , stay Cocos2d—X In the engine , This is an imitation iOS Inside NSTableView A control of .

S1, To use this control, you need to import the extension library file first

#include "cocos-ext.h"

S2, Then make the class inherit CCTableView Of 2 A broker ,

class ServerPrizeList :public CCLayer,public CCTableViewDataSource,public CCTableViewDelegate

Take a look first dataSource , The method in this will be the main method we use :

class CCTableViewDataSource
virtual ~CCTableViewDataSource() {} /**
* cell size for a given index
* The size of each cell , According to idx To personalize the cell size
* @param idx the index of a cell to get a size
* @return size of a cell at given index
virtual CCSize tableCellSizeForIndex(CCTableView *table, unsigned int idx) {
return cellSizeForTable(table);
* cell height for a given table.
* This method is generally used , Custom cell size , It can't be based on idx To customize
* @param table table to hold the instances of Class
* @return cell size
virtual CCSize cellSizeForTable(CCTableView *table) {
return CCSizeZero;
* a cell instance at a given index
* This method makes the main method of data source , Initialization data is in this method
* @param idx index to search for a cell
* @return cell found at idx
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx) = 0;
* Returns number of cells in a given table view.
* return tableView Of cells
* @return number of cells
virtual unsigned int numberOfCellsInTableView(CCTableView *table) = 0; };

then , look down CCTableViewDelegate Methods :

class CCTableViewDelegate : public CCScrollViewDelegate
* Delegate to respond touch event
* Realize the touch event response of clicking cell , Make the main use of
* @param table table contains the given cell
* @param cell cell that is touched
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell) = 0; /**
* Delegate to respond a table cell press event.
* Set the cell highlight state
* @param table table contains the given cell
* @param cell cell that is pressed
virtual void tableCellHighlight(CCTableView* table, CCTableViewCell* cell){}; /**
* Delegate to respond a table cell release event
* Set the non highlighted state of the cell
* @param table table contains the given cell
* @param cell cell that is pressed
virtual void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell){}; /**
* Delegate called when the cell is about to be recycled. Immediately
* after this call the cell will be removed from the scene graph and
* recycled.
* Loop through a cell
* @param table table contains the given cell
* @param cell cell that is pressed
virtual void tableCellWillRecycle(CCTableView* table, CCTableViewCell* cell){}; };

Of course , We need to achieve 2 A way :

// because cctableview Inherited from ccscrollview, So to implement these two methods , But do nothing 
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);

well , Generally, the following methods are enough to solve the problem :

 virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell);
virtual CCSize cellSizeForTable(CCTableView *table);
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx);
virtual unsigned int numberOfCellsInTableView(CCTableView *table);

S3, And then let's do it in .cpp These proxy methods are implemented in the file . Most of the implementation methods are omitted , Let's talk about

CCTableViewCell* ServerPrizeList::tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx)

Follow the examples from the official website and other articles , It's possible to diversify the text in each cell .

You can see that the text in the middle is OK Of , But there are problems on both sides , On the left is a picture that can't be heterogeneous , On the right is the exchange idx Incorrect acquisition .

Now we've solved the problem of exchange idx The problem of , The beginning is when you use add cells , Turn this button on tag Set to the idx.

But the problem is ,idx In this button, it's chaotic and disordered . Can't get cell's idx.

CCMenuItemImage *itemImage = CCMenuItemImage::create("22.png", "22.png", this, menu_selector(ServerPrizeList::menuItemCall2));
//tag The default is -1, This is the way to get tag, Namely idx Is chaotic 
if (itemImage->getTag() == -1) {
itemImage->setPosition(ccp(tableCellSize.width*0.75, tableCellSize.height/2)); CCMenu *menu = CCMenu::create(itemImage,NULL);
pCell->addChild(menu, 1);

later , notice

tableCellTouched(cocos2d::extension::CCTableView *table, cocos2d::extension::CCTableViewCell *cell)

All of a sudden there was inspiration , You can get the parent class of the button's parent class , Namely Cell To get the idx, ha-ha , The problem is finally solved .

CCTableViewCell *cell = (CCTableViewCell*)(((CCMenuItemImage*)pSender)->getParent()->getParent());
//getIdx() This method can get the array subscript of the click cell
CCLog("idx: %d",cell->getIdx());

Now? , There are still problems on the left , If the great God sees , Welcome to teach me .

2013-07-24 18:03:26.122   The problem has been solved , The texture is as follows :

Cocos2d—X Game development CCTableView Detailed explanation ( 11、 ... and ) More articles about

  1. Android Development of InstanceState Detailed explanation

    Android Development of InstanceState Detailed explanation   In this paper, Android About China Activity The two mysterious ways of :onSaveInstanceState() and onRestoreInstanceS ...

  2. Android Development of InstanceState Detailed explanation ( turn )--- Use it to preserve Activity state

    Android Development of InstanceState Detailed explanation   In this paper, Android About China Activity The two mysterious ways of :onSaveInstanceState() and onRestoreInstanceS ...

  3. Cocos2d—X Game development CCToggle( Menu tab switching )CCControlSwitch( Switch switching )

    Cocos2d—X Game development CCToggle( Menu tab switching ) First of all, the heirs CCMenu, It's one of the menu tags .‘ class CC_DLL CCMenuItemToggle : public CCMenu ...

  4. Android Development of MdiaPlayer Detailed explanation

    Android Development of MdiaPlayer Detailed explanation MediaPlayer Class can be used to control audio / Playing video files or streams , I was there <Android The development is based on Service The music player > It's used in this article . ...

  5. IOS Development of UINavigationController Detailed explanation

    UINavigationController yes IOS A kind of container commonly used in programming view controller, Many system controls ( Such as UIImagePickerViewController) And a lot of famous AP ...

  6. ios Development of NSObject Detailed explanation

    NSObject Most of them Objective-C The root class of the class inheritance system . This class follows NSObject agreement , Provides some general methods , Object by inheritance NSObject, The interface from which you can inherit access to the runtime , And let the object have Obje ...

  7. Cocos2d—X Game development CCScrollView( Slide view )( Twelve )

    CCScrollView stay Cocos2d-X The engine is mainly used when the image size is much larger than the screen size . On the whole , It's easy to use . One is CCScrollView Control itself , One is CCScrollViewDe ...

  8. android Development of service Detailed explanation

    service As android One of the four components of , Its importance can be imagined , In development , We often put work that doesn't need to interact with users in service Middle to finish ,service Running in the background , In this way, some people may have the illusion , think ...

  9. Android Development of InstanceState Detailed explanation ( turn )

    This article is from :http://www.cnblogs.com/hanyonglu/archive/2012/03/28/2420515.html In this paper, Android About China Activity The two mysterious ways of : ...

Random recommendation

  1. ls The contents of the catalog

    ls: List contents 1. Common parameters : -a, –all  List all files in the directory , Include  .  The implied file at the beginning ( Commonly used ) -A  Same as -a, But not listed “.”( Represents the current directory ) and “..”( Represents the parent directory of the current directory ). -c  ...

  2. Move web Development preparation knowledge points

    1.1.1  Fluid layout Actually   Fluid layout   It's the percentage layout , Set the width of the box as a percentage to scale according to the width of the screen , Not limited by fixed pixels , The content is padded on both sides . This layout   It's mobile. web Common layout used in development ...

  3. tomcat6 Class loader and class loading order

    tomcat6.0.32 com.dyyx.ShareUtils// Returns the current time of the system public static String now(); package com.dyyx;import java.t ...

  4. console, And define events for yourself

    console.log This command outputs the log in the browser console , Used to debug programs Follow alert similar But not like alert That would interrupt the program .

  5. C Solve a high school problem that Baidu knows

    Preface Today I saw a question from Baidu , That's true . After careful calculation ,  It took 30min. It just came out , It is estimated that I will go back to take part in the college entrance examination now , Passing math is up in the air . Sometimes it's no use trying to do such a question , What's the point of learning these things , What's the point of wasting time on this ...

  6. SqlServer in DateTime Turn the string

    Select CONVERT(varchar(100), GETDATE(), 8):14:53:14 Select CONVERT(varchar(100), GETDATE(), 9): 06  ...

  7. myeclipse project Can't open

    After restarting the computer , myeclipse project It can't be opened , It was good before !! appear : Failed to read the project description file (.project) for ' ...

  8. linux 3.10 once softlock screening

    x86 framework . A colleague analyzed crash, On the basis of him, I assist in the analysis again , And there's no progress , Just record the analysis . A record is a memo , In case someone solves it , Give us some help . There was a soft lock , majority cpu Locked ,log The first one in the book thinks that ...

  9. .net turn PHP Starting from scratch - To configure visual studio 2013 PHP development environment php for visual studio

    As a .net developer , Has been visual studio This powerful editor favors , No other editor can use , I'm not familiar with it . Recently, this is not a turn php Do you , Use php The editor was upset , I think it's still useful visual studio comfortable ...

  10. Eigen course (11)

    Tidy up Eigen Library Tutorial , Reference resources :http://eigen.tuxfamily.org/dox/index.html Storage order There are two ways to store matrices and two-dimensional arrays , Column first and row first . Hypothetical matrix : Priority by line ...