Cocos2dx learning-cctableview

Source: Internet
Author: User

The last time I summarized ccscrollview, I always wanted to summarize cctableview, but I was too lazy to move it. I had to debug the Code with my colleagues tonight and take the time when my colleagues changed the bug, let's briefly summarize it.

Cctableview is generally used in games in scenarios or layers such as backpacks. Of course, it is more than that. In iOS development, uitableview is widely used. Of course, they are used in the same way. (In fact, you will find that cctableview is actually written with reference to uitableview, so the IOS developers are happy ).

Now that we have used our backpack, let's take a simple backpack information example to learn about it.

Add the. h file to the code first.

 

//

// Cctableviewlayer. h

// Cocos2dxlearndemo

//

// Created by Ding Changxing on 12-12-31.

//

//

 

# Ifndef _ cocos2dxlearndemo _ cctableviewlayer __

# DEFINE _ cocos2dxlearndemo _ cctableviewlayer __

 

# Include <iostream>

# Include "cocos2d. H"

# Include "cocos-ext.h"

Using_ns_cc;

Using_ns_cc_ext;

 

Class cctableviewlayer: publiccclayer, publiccctableviewdatasource, publiccctableviewdelegate {

Public:

// Method 'init 'in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)

Virtual bool Init ();

// Preprocessor macro for "static create ()" Constructor (node () Deprecated)

Create_func (cctableviewlayer );

 

// Datasource

/**

* Cell height for a given table.

*

* @ Param table to hold the instances of class

* @ Return cell size

*/

Virtual ccsize cellsizefortable (cctableview * table );

/**

* A cell instance at a given Index

*

* @ Param idx index to search for a cell

* @ Return cell found at idx

*/

Virtual cctableviewcell * tablecellatindex (cctableview * Table, unsigned int idx );

/**

* Returns number of cells in a given table view.

*

* @ Return number of cells

*/

Virtual unsigned int numberofcellsintableview (cctableview * table );

// Delegate

Virtual void tablecelltouched (cctableview * Table, cctableviewcell * cell );

// The ccscrollviewdelegate virtual function is also introduced because cctableview inherits from ccscrollview.

Virtual void scrollviewdidscroll (cocos2d: Extension: ccscrollview * view );

Virtual void scrollviewdidzoom (cocos2d: Extension: ccscrollview * view );

};

 

# Endif/* defined (_ cocos2dxlearndemo _ cctableviewlayer __)*/

 

. M file

//

// Cctableviewlayer. cpp

// Cocos2dxlearndemo

//

// Created by Ding Changxing on 12-12-31.

//

//

 

# Include "cctableviewlayer. H"

# Include "testsprite. H"

Boolcctableviewlayer: Init (){

If (! Cclayer: Init ()){

Returnfalse;

}

// The first parameter is datasource.

Cctableview * tableview = cctableview: Create (this, ccsizemake (480,240 ));

Tableview-> setposition (CCP (0, 0 ));

// Set the direction

Tableview-> setdirection (kccscrollviewdirectionvertical );

Tableview-> setanchorpoint (ccpointzero );

// Tableview-> setanchorpoint (CCP (0.5, 0.5 ));

Tableview-> setposition (CCP (0, 50 ));

Tableview-> setdelegate (this );

Cclog ("anchpoint = % F, % F", tableview-> getanchorpoint (). X, tableview-> getanchorpoint (). y );

// Set the order from top to bottom

Tableview-> setverticalfillorder (kcctableviewfilltopdown );

// Cclog ("tableview-> getposition () = % F, % F", tableview-> getposition (). X, tableview-> getposition (). y );

This-> addchild (tableview );

// Cclog ("afteranchpoint = % F, % F", tableview-> getanchorpoint (). X, tableview-> getanchorpoint (). y );

Tableview-> reloaddata ();

This-> settouchenabled (true );

Returntrue;

}

// Datasource

 

Ccsizecctableviewlayer: cellsizefortable (cctableview * Table ){

Return ccsizemake (Table-& gt; getcontentsize (). Width, 100 );

}

Cctableviewcell * cctableviewlayer: tablecellatindex (cctableview * Table, unsignedint idx ){

Cctableviewcell * cell = table-> dequeuecell ();

If (! Cell ){

Cell = new cctableviewcell ();

Cell-> autorelease ();

}

Cell-> removeallchildrenwithcleanup (true );

For (INT I = 0; I <3; I ++ ){

Ccsprite * testsprite = ccsprite: Create ("icon.png ");

Testsprite-> setposition (CCP (240 + 100 * I, 30 ));

Cell-> addchild (testsprite );

}

Return cell;

}

 

Unsigned int cctableviewlayer: numberofcellsintableview (cctableview * Table ){

Return 20;

}

 

// Delegate

Voidcctableviewlayer: tablecelltouched (cctableview * Table, cctableviewcell * cell ){

// Cclog ("hhhhhhhh ");

}

 

Void cctableviewlayer: scrollviewdidscroll (ccscrollview * view ){

// Cclog ("scrollviewdidscroll ");

}

// Call when setting zoom

Void cctableviewlayer: scrollviewdidzoom (ccscrollview * view ){

// Cclog ("scrollviewdidzoom ");

}

 

 

First, cctableview inherits from ccscrollview. Naturally, it is in the cocosd-x extension library.

So you will find that you want to introduce the following two virtual functions.

Virtual void scrollviewdidscroll (cocos2d: Extension: ccscrollview * view ){}

Virtual void scrollviewdidzoom (cocos2d: Extension: ccscrollview * view ){}

In addition, its usage is basically the same as that of uitableview. Of course, we need to inherit

Publiccctableviewdatasource, publiccctableviewdelegate.

Several methods are similar to those of uitableview.

// Cctableviewdatasource

Ccsizecctableviewlayer: cellsizefortable (cctableview * table)

Unsigned int cctableviewlayer: numberofcellsintableview (cctableview * table)

Cctableviewcell * cctableviewlayer: tablecellatindex (cctableview * Table, unsignedint idx)

// Cctableviewdelegate

Void cctableviewlayer: tablecelltouched (cctableview * Table, cctableviewcell * cell)

 

Of course. The general look of our backpack came out. However, it shows that an item has no other functions. Because most backpacks support double-click and long-press. This will be implemented later.

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.