newlist

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

该类已经把tableview封装好,可以把它当做一个精灵来用,这样做的好处是,当一个界面同时需要多个tableview的时候就可以很好的解决这个问题,而且模块也更清晰。

//-----------------------------------------------------------------
//@file        gameui/Card/CardSlip.h
//@date        2014-01-03
//@desc        我的卡组滑动界面
//@action    我的卡组系统 
//-----------------------------------------------------------------

#ifndef __GAMEUI_CARD_CARDSLIP_H__
#define __GAMEUI_CARD_CARDSLIP_H__

#include <vector>
#include "include/ILayer.h"
#include "kernel/BaseDefine.h"
#include "cocos-ext.h"

USING_NS_CC_EXT;

class CCardSlip : public CBaseLayer, public CCTableViewDataSource, public CCTableViewDelegate
{
private:
    CCTableView* m_pTableView;
    CCTableViewCell* m_pClickCell;
    CCSprite* m_pSelect;
    int m_nSelectIndex;
    CCPoint m_ccSelectPoint;

private:
    bool CreateCell(CCTableViewCell* pCell, UINT unIdx);
    bool ReCreateCell(CCTableViewCell* pCell, UINT unIdx);
    void ClickContent(UINT unIdx);

public:
    virtual bool Create();
    virtual void OnTick() {}
    virtual void onEnter();
    virtual void onEnterTransitionDidFinish();
    virtual void onExit();

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

    virtual void             scrollViewDidScroll(CCScrollView* view);
    virtual void             scrollViewDidZoom(CCScrollView* view);

};

#endif // __GAMEUI_CARD_CARDSLIP_H__
#include "CardSlip.h"

#include "kernel/Common.h"
#include "kernel/GlobalMgr.h"
#include "kernel/CommonData/CommonData.h"

bool CCardSlip::Create()
{
    if (!CBaseLayer::Create())
        return false;
    setTouchEnabled(true);
}

void CCardSlip::onEnter()
{
    CBaseLayer::onEnter();
}

void CCardSlip::onEnterTransitionDidFinish()
{
    CBaseLayer::onEnterTransitionDidFinish();

    m_pTableView = CCTableView::create(this, CCSizeMake(277, 465));
    CC_ERROR(m_pTableView, "【CCardSlip::onEnterTransitionDidFinish】m_pTableView create failed")

    m_pTableView->setAnchorPoint(CCPointZero);
    m_pTableView->setDirection(kCCScrollViewDirectionVertical);
    m_pTableView->setPosition(CCPointZero);
    m_pTableView->setDelegate(this);
    m_pTableView->setVerticalFillOrder(kCCTableViewFillTopDown);
    this->addChild(m_pTableView);
    m_pTableView->reloadData();
}

void CCardSlip::onExit()
{
    CBaseLayer::onExit();
}

bool CCardSlip::CreateCell(CCTableViewCell* pCell, UINT unIdx)
{
    std::string g_ImgPath(CGlobalMgr::GetInstance()->GetResourcesEx());
    std::string strPath;

    strPath = g_ImgPath + "29.png";
    CCSprite* pImgSlot = CCSprite::create(strPath.c_str());
    CC_ERROR_R(pImgSlot, "【CCardSlip::CreateCell】pImgSlot 为空")
    pImgSlot->setPosition(CCPointZero);
    pImgSlot->setAnchorPoint(CCPointZero);
    pCell->addChild(pImgSlot);
    return true;
}

bool CCardSlip::ReCreateCell(CCTableViewCell* pCell, UINT unIdx)
{
    std::string g_ImgPath(CGlobalMgr::GetInstance()->GetResourcesEx());
    std::string strPath;

    strPath = g_ImgPath + "29.png";
    CCSprite* pImgSlot = CCSprite::create(strPath.c_str());
    CC_ERROR_R(pImgSlot, "【CCardSlip::ReCreateCell】pImgSlot 为空")
    pImgSlot->setPosition(CCPointZero);
    pImgSlot->setAnchorPoint(CCPointZero);
    pCell->addChild(pImgSlot);
    return true;
}

void CCardSlip::ClickContent(UINT unIdx)
{

}

void CCardSlip::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
    if(!table || !cell)
        return;

    m_pClickCell = cell;
}

CCSize CCardSlip::cellSizeForTable(CCTableView *table)
{
    return CCSizeMake(280, 62);
}

CCTableViewCell* CCardSlip::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    CCTableViewCell* pCell = table->dequeueCell();
    if (!pCell)
    {
        pCell = new CCTableViewCell();
        pCell->autorelease();
        this->CreateCell(pCell, idx);
    }
    else
    {
        pCell->removeAllChildren();
        this->ReCreateCell(pCell, idx);
    }
    return pCell;
}

unsigned int CCardSlip::numberOfCellsInTableView(CCTableView *table)
{
    return 10;
}

void CCardSlip::scrollViewDidScroll(CCScrollView* view)
{

}

void CCardSlip::scrollViewDidZoom(CCScrollView* view)
{

}

 

posted on 2014-01-04 11:28  一枚程序  阅读(393)  评论(0编辑  收藏  举报