2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "TableViewTestScene.h"
|
|
|
|
#include "CustomTableViewCell.h"
|
|
|
|
#include "../ExtensionsTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
USING_NS_AX_EXT;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TableViewTests::TableViewTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(TableViewTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
// on "init" you need to initialize your instance
|
|
|
|
bool TableViewTest::init()
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (!TestCase::init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Size winSize = Director::getInstance()->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
TableView* tableView = TableView::create(this, Size(250.0f, 60.0f));
|
|
|
|
tableView->setDirection(ScrollView::Direction::HORIZONTAL);
|
2021-12-31 12:12:40 +08:00
|
|
|
tableView->setPosition(Vec2(20.0f, winSize.height / 2 - 30));
|
2019-11-23 20:27:39 +08:00
|
|
|
tableView->setDelegate(this);
|
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
|
|
|
|
|
|
|
auto testNode = Node::create();
|
|
|
|
testNode->setName("testNode");
|
|
|
|
tableView->addChild(testNode);
|
|
|
|
tableView->removeChild(testNode, true);
|
|
|
|
CCAssert(nullptr == tableView->getChildByName("testNode"), "The added child has been removed!");
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
tableView = TableView::create(this, Size(60.0f, 250.0f));
|
|
|
|
tableView->setDirection(ScrollView::Direction::VERTICAL);
|
|
|
|
tableView->setPosition(Vec2(winSize.width - 150, winSize.height / 2 - 120));
|
|
|
|
tableView->setDelegate(this);
|
|
|
|
tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
|
|
|
|
this->addChild(tableView);
|
|
|
|
tableView->reloadData();
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableViewTest::tableCellTouched(TableView* table, TableViewCell* cell)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("cell touched at index: %d", static_cast<int32_t>(cell->getIdx()));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Size TableViewTest::tableCellSizeForIndex(TableView* table, ssize_t idx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
if (idx == 2)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return Size(100, 100);
|
|
|
|
}
|
|
|
|
return Size(60, 60);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
TableViewCell* TableViewTest::tableCellAtIndex(TableView* table, ssize_t idx)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto string = StringUtils::format("%d", static_cast<int32_t>(idx));
|
|
|
|
TableViewCell* cell = table->dequeueCell();
|
|
|
|
if (!cell)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
cell = new CustomTableViewCell();
|
2019-11-23 20:27:39 +08:00
|
|
|
cell->autorelease();
|
|
|
|
auto sprite = Sprite::create("Images/Icon.png");
|
|
|
|
sprite->setAnchorPoint(Vec2::ZERO);
|
|
|
|
sprite->setPosition(Vec2(0, 0));
|
|
|
|
cell->addChild(sprite);
|
|
|
|
|
|
|
|
auto label = Label::createWithSystemFont(string, "Helvetica", 20.0);
|
|
|
|
label->setPosition(Vec2::ZERO);
|
2021-12-31 12:12:40 +08:00
|
|
|
label->setAnchorPoint(Vec2::ZERO);
|
2019-11-23 20:27:39 +08:00
|
|
|
label->setTag(123);
|
|
|
|
cell->addChild(label);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto label = (Label*)cell->getChildByTag(123);
|
|
|
|
label->setString(string);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
ssize_t TableViewTest::numberOfCellsInTableView(TableView* table)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return 20;
|
|
|
|
}
|