2012-09-09 22:34:32 +08:00
|
|
|
#include "TableViewTestScene.h"
|
|
|
|
#include "CustomTableViewCell.h"
|
|
|
|
#include "../ExtensionsTest.h"
|
|
|
|
|
|
|
|
USING_NS_CC;
|
|
|
|
USING_NS_CC_EXT;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TableViewTests::TableViewTests()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
ADD_TEST_CASE(TableViewTest);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// on "init" you need to initialize your instance
|
2015-04-03 14:31:03 +08:00
|
|
|
bool TableViewTest::init()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if ( !TestCase::init() )
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->getWinSize();
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
TableView* tableView = TableView::create(this, Size(250, 60));
|
2013-07-26 22:55:41 +08:00
|
|
|
tableView->setDirection(ScrollView::Direction::HORIZONTAL);
|
2014-05-15 01:07:09 +08:00
|
|
|
tableView->setPosition(Vec2(20,winSize.height/2-30));
|
2012-09-09 22:34:32 +08:00
|
|
|
tableView->setDelegate(this);
|
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
|
|
|
|
2013-07-12 14:30:26 +08:00
|
|
|
tableView = TableView::create(this, Size(60, 250));
|
2013-07-26 22:55:41 +08:00
|
|
|
tableView->setDirection(ScrollView::Direction::VERTICAL);
|
2014-05-15 01:07:09 +08:00
|
|
|
tableView->setPosition(Vec2(winSize.width-150,winSize.height/2-120));
|
2012-09-09 22:34:32 +08:00
|
|
|
tableView->setDelegate(this);
|
2013-07-26 22:55:41 +08:00
|
|
|
tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void TableViewTest::tableCellTouched(TableView* table, TableViewCell* cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-11-18 15:55:58 +08:00
|
|
|
CCLOG("cell touched at index: %ld", cell->getIdx());
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
Size TableViewTest::tableCellSizeForIndex(TableView *table, ssize_t idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-04-10 16:38:00 +08:00
|
|
|
if (idx == 2) {
|
2013-07-12 14:30:26 +08:00
|
|
|
return Size(100, 100);
|
2013-04-10 16:38:00 +08:00
|
|
|
}
|
2013-07-12 14:30:26 +08:00
|
|
|
return Size(60, 60);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
TableViewCell* TableViewTest::tableCellAtIndex(TableView *table, ssize_t idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2015-07-15 12:04:48 +08:00
|
|
|
auto string = StringUtils::format("%ld", idx);
|
2013-06-20 14:17:10 +08:00
|
|
|
TableViewCell *cell = table->dequeueCell();
|
2012-09-09 22:34:32 +08:00
|
|
|
if (!cell) {
|
2014-08-28 07:31:57 +08:00
|
|
|
cell = new (std::nothrow) CustomTableViewCell();
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->autorelease();
|
2013-08-16 16:05:27 +08:00
|
|
|
auto sprite = Sprite::create("Images/Icon.png");
|
2014-05-15 01:07:09 +08:00
|
|
|
sprite->setAnchorPoint(Vec2::ZERO);
|
|
|
|
sprite->setPosition(Vec2(0, 0));
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->addChild(sprite);
|
|
|
|
|
2015-07-15 12:04:48 +08:00
|
|
|
auto label = Label::createWithSystemFont(string, "Helvetica", 20.0);
|
2014-05-15 01:07:09 +08:00
|
|
|
label->setPosition(Vec2::ZERO);
|
|
|
|
label->setAnchorPoint(Vec2::ZERO);
|
2012-09-09 22:34:32 +08:00
|
|
|
label->setTag(123);
|
|
|
|
cell->addChild(label);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-26 23:33:58 +08:00
|
|
|
auto label = (Label*)cell->getChildByTag(123);
|
2015-07-15 12:04:48 +08:00
|
|
|
label->setString(string);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
ssize_t TableViewTest::numberOfCellsInTableView(TableView *table)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
return 20;
|
|
|
|
}
|