Cocos2d—X游戏开发之CCTableView详解(十一)

本来很早就想写关于CCTableView的文章,但是在基本功能实现之后呢,项目需求增加导致对这个控件的研究必须更加深入一点.

好的,现在开始介绍一下这个控件,在Cocos2d—X引擎中,这是一个仿制iOS里面的NSTableView的一个控件。


S1,使用这个控件需要首先引入扩展库文件

#include "cocos-ext.h"


S2,然后使类继承CCTableView的2个代理,

class ServerPrizeList :public CCLayer,public CCTableViewDataSource,public CCTableViewDelegate


首先看下 dataSource ,这个里面的方法将是我们使用的主要方法:

class CCTableViewDataSource
{
public:
    virtual ~CCTableViewDataSource() {}

    /**
     * cell size for a given index
     * 每个单元格的尺寸,可以根据idx来个性化定制单元格的尺寸
     * @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.
     * 一般使用这个方法,定制单元格尺寸,不可以根据idx来定制
     * @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
     * 这个方法使数据源的主要方法,初始化数据在这个方法里面
     * @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.
     * 返回tableView的单元格数量
     * @return number of cells
     */
    virtual unsigned int numberOfCellsInTableView(CCTableView *table) = 0;

};


然后,看下CCTableViewDelegate的方法:

class CCTableViewDelegate : public CCScrollViewDelegate
{
public:
    /**
     * Delegate to respond touch event
     * 实现点击单元格的触摸事件响应,使主要使用方法
     * @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.
     * 设置单元格高亮的状态
     * @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
     * 设置单元格非高亮的状态
     * @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.
     * 循环使用某个单元格
     * @param table table contains the given cell
     * @param cell  cell that is pressed
     */
    virtual void tableCellWillRecycle(CCTableView* table, CCTableViewCell* cell){};

};


当然,还要实现2个方法:

//因为cctableview继承自ccscrollview,所以要实现这两个方法,但是什么都不做
    virtual void scrollViewDidScroll(CCScrollView* view);
    virtual void scrollViewDidZoom(CCScrollView* view);


好的,一般继承下面的几个方法就足以解决问题:


    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,然后让我们在。cpp文件里面实现这些代理方法。实现的方法大部分略过,主要使讲下

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


按照官方网站的示例和其他文章的例子,在单元格中实现每个单元格的文字的多样化完全可行。



大家可以看到中间的文字是OK的,但是两边的问题就有了,左边是图片不能实现异样化,右边是兑换的idx获取不正确。

现在解决了兑换的idx的问题,开始是使用添加单元格的时候,把这个按钮的tag设置为单元格的idx。

但是问题是,idx在这个按钮里面是混乱且无序的。不能正确获取单元格的idx。

CCMenuItemImage *itemImage = CCMenuItemImage::create("22.png", "22.png", this, menu_selector(ServerPrizeList::menuItemCall2));
        
        //tag默认为-1,通过这个方法得到的tag,就是idx是无序的
        if (itemImage->getTag() == -1) {
            itemImage->setTag(idx);
        }
        itemImage->setPosition(ccp(tableCellSize.width*0.75, tableCellSize.height/2));
        
        CCMenu *menu = CCMenu::create(itemImage,NULL);
        menu->setPosition(CCPointZero);
        pCell->addChild(menu, 1);



后来,看到  

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

突然有了灵感,可以获取按钮的父类的父类,就是Cell来获取点击的idx,哈哈,问题终于解决了。

CCTableViewCell *cell = (CCTableViewCell*)(((CCMenuItemImage*)pSender)->getParent()->getParent());
    //getIdx()这个方法可以获取点击单元格的数组下标
    CCLog("idx: %d",cell->getIdx());


现在,还有左边的问题要解决,如有大神看到,欢迎赐教啊。

2013-07-24 18:03:26.122  问题已经解决,贴图如下:








原文地址:https://www.cnblogs.com/javawebsoa/p/3211899.html