注意点:
1、其中的listView和Tableview都是继承自Scrollview
2、本文主要讲解的是tableview
3、代码部分引用的是在公司中做的产品
4、以上代码可以修改的地方:将cell改为继承自TableviewCell,这样可以直接创建cell。
5、代码中使用了函数的绑定和函数的回调(利用代理模式也可以实现)
使用cocostudio制作。
6、注意的地方是:reloadData()需要在所有的数据加载之后进行调用。
7、cell若是多个GUI控件的话,需要设置控件的touch属性,吞噬设置为false,否则cell无法点击;可以设置cell继承自TableviewCell
8、tableview_是添加到this(当前的层上时,滑动效果较好,放到层上的独立的UI控件,滑动效果不好)
具体的调用的三个虚函数参考博客外部链接
博客的外部链接:http://www.2cto.com/kf/201408/329311.html
以下是在源码:
<span style="font-size:14px;">头文件:
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
#include "cocos-ext.h"
using namespace cocos2d;
using namespace cocos2d::ui;
using namespace extension;
class LayerAreaIdentify : public BaseLayer, public TableViewDataSource, public TableViewDelegate
{
public:
CREATE_FUNC(LayerAreaIdentify);
virtual bool init();
#pragma mark tableview datasource --
cocos2d::Size tableCellSizeForIndex(TableView *table, ssize_t idx);
TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx);
ssize_t numberOfCellsInTableView(TableView *table);
#pragma mark tableview delegate --
void tableCellTouched(TableView* table, TableViewCell* cell);
void btn_back_call_back(Ref * sender);
#pragma mark otherfunction
void image_click_callBack(Ref * sender);
void set_nodecell_call_back(std::function<void(std::string str)> call_back){
nodecell_call_back = call_back;
}
private:
Node * root_;
cocos2d::ui::Button * btn_back;
ListView* listview_;
TableView * tableview_;
std::vector<std::string> _nationVector;
std::vector<std::string> _numberVector;
void removeSelf_updateInfo();
LayerAreaIdentify * _layer_self;
bool isScrolling;
float touchBeginPosY;
std::function<void(std::string str)> nodecell_call_back;
};
源文件:
#include "LayerAreaIdentify.h"
#include "NodePhoneAreaCell.h"
bool LayerAreaIdentify::init()
{
if(!BaseLayer::init()){
return false;
}
root_ = CSLoader::createNode("LayerAreaIdentify.csb");
this->addChild(root_);
btn_back = dynamic_cast<cocos2d::ui::Button *>(CSLoader::seekNodeByName(root_, "btn_back"));
btn_back->addClickEventListener(std::bind(&LayerAreaIdentify::btn_back_call_back, this,std::placeholders::_1));
listview_ = dynamic_cast<cocos2d::ui::ListView *>(CSLoader::seekNodeByName(root_, "listview"));
tableview_ = TableView::create(this, listview_->getContentSize());
tableview_->setAnchorPoint(listview_->getAnchorPoint());
tableview_->setContentOffset(Vec2(0, 0));
tableview_->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
tableview_->setPosition(listview_->getPosition());
tableview_->setDelegate(this);
//夹持到边界
tableview_->setClippingToBounds(true);
tableview_->setDataSource(this);
tableview_->setVisible(true);
tableview_->setLocalZOrder(10);
root_->addChild(tableview_);
tableview_->reloadData();
listview_->removeFromParent();
this->setVisible(true);
return true;
}
cocos2d::Size LayerAreaIdentify::tableCellSizeForIndex(TableView *table, ssize_t idx){
Size cellSize = Size(750,100);
return cellSize;
}
TableViewCell* LayerAreaIdentify::tableCellAtIndex(TableView *table, ssize_t idx){
TableViewCell * cell = table->dequeueCell();
if(!cell)
{
cell = new TableViewCell();
cell->autorelease();
}
cell->removeAllChildren();
auto imageCell = NodePhoneAreaCell::create();
imageCell->image_selec_call_back(std::bind(&LayerAreaIdentify::image_click_callBack,this,std::placeholders::_1));
imageCell->setPosition(Vec2(0, 0));
imageCell->setAnchorPoint(Vec2(0, 0));
imageCell->set_text_nation("UK");
imageCell->set_text_number("186");
imageCell->set_image_tag(10001);
cell->addChild(imageCell);
if(idx == 12){
imageCell->set_image_tag(1234);
}
//cell->update_data(room_type_datas_[idx]);
// auto listener = EventListenerTouchOneByOne::create();
// listener->setSwallowTouches(false);
// listener->onTouchBegan = CC_CALLBACK_2(LayerAreaIdentify::onTouchBegan, this);
// listener->onTouchMoved = CC_CALLBACK_2(LayerAreaIdentify::onTouchMoved, this);
// listener->onTouchEnded = CC_CALLBACK_2(LayerAreaIdentify::onTouchEnded, this);
// _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, cell);
return cell;
}
ssize_t LayerAreaIdentify::numberOfCellsInTableView(TableView *table){
return 30;
}
void LayerAreaIdentify::tableCellTouched(TableView* table, TableViewCell* cell){
if(cell->getTag() == 10001){
CCLOG("########NodePhoneAreaCell is touched");
}
// _layer_self->removeFromParent();
}
void LayerAreaIdentify::btn_back_call_back(Ref * sender){
this->removeFromParent();
}
void LayerAreaIdentify::removeSelf_updateInfo(){
this->removeFromParent();
}
void LayerAreaIdentify::image_click_callBack(Ref * sender)
{
auto imageBg = dynamic_cast<cocos2d::ui::ImageView *>(sender);
imageBg->loadTexture("res/login_res/Phone_area/BTN_quyu_pressed.png");
CCLOG("%d",imageBg->getTag());
if(nodecell_call_back){
// char str[20];
// sprintf(str,"%d",imageBg->getTag());
nodecell_call_back(to_string(imageBg->getTag()));
}
this->removeFromParent();
}
//相应的cell文件
头文件:
#include "cocos2d.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
#include "cocos-ext.h"
#include "LayerChooseGameType.h"
using namespace cocos2d;
using namespace cocos2d::ui;
using namespace extension;
#include <stdio.h>
class NodePhoneAreaCell : public Widget {
public:
CREATE_FUNC(NodePhoneAreaCell);
virtual bool init();
void update_data();
void image_selec_call_back(std::function<void(Ref * sender)> value);
void set_text_nation(const std::string &text){
text_nation->setString(text);
}
const std::string& get_text_nation()
{
return text_nation->getString();
}
void set_text_number(const std::string &text){
text_number->setString(text);
}
const std::string & get_text_number(){
return text_number->getString();
}
void set_image_tag(int tag){
image_select->setTag(tag);
}
int get_image_tag(){
image_select->getTag();
}
private:
cocos2d::ui::ImageView * image_select;
cocos2d::ui::Text * text_nation;
cocos2d::ui::Text * text_number;
Node * root_;
};
源文件:
#include "NodePhoneAreaCell.h"
bool NodePhoneAreaCell::init(){
if (!Widget::init()) {
return false;
}
root_ = CSLoader::createNode("NodePhoneArea.csb");
this->addChild(root_);
image_select = dynamic_cast<cocos2d::ui::ImageView*>(CSLoader::seekNodeByName(root_,"Image_bg"));
image_select->loadTexture("res/login_res/Phone_area/BTN_quyu_nor.png");
image_select->setTouchEnabled(true);
image_select->setSwallowTouches(false);
text_nation = dynamic_cast<cocos2d::ui::Text *>(CSLoader::seekNodeByName(root_, "Text_nation"));
text_number = dynamic_cast<cocos2d::ui::Text *>(CSLoader::seekNodeByName(root_, "Text_num"));
return true;
}
void NodePhoneAreaCell::update_data(){
}
void NodePhoneAreaCell::image_selec_call_back(std::function<void(Ref * sender)> value)
{
// char numStr[20];
// auto str = sprintf(numStr, "%d",image_select->getTag());
//image_select->setTag(<#int tag#>)
image_select->setName(this->get_text_nation());
image_select->addClickEventListener(value);
}</span>