PuzzleSDK
TableViewTestScene.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "TableViewTestScene.h"
26#include "CustomTableViewCell.h"
27#include "../ExtensionsTest.h"
28
31
32TableViewTests::TableViewTests()
33{
35}
36
37// on "init" you need to initialize your instance
39{
40 if ( !TestCase::init() )
41 {
42 return false;
43 }
44
45 Size winSize = Director::getInstance()->getWinSize();
46
47 TableView* tableView = TableView::create(this, Size(250.0f, 60.0f));
48 tableView->setDirection(ScrollView::Direction::HORIZONTAL);
49 tableView->setPosition(Vec2(20.0f,winSize.height/2-30));
50 tableView->setDelegate(this);
51 this->addChild(tableView);
52 tableView->reloadData();
53
54 auto testNode = Node::create();
55 testNode->setName("testNode");
56 tableView->addChild(testNode);
57 tableView->removeChild(testNode, true);
58 CCAssert(nullptr == tableView->getChildByName("testNode"), "The added child has been removed!");
59
60
61 tableView = TableView::create(this, Size(60.0f, 250.0f));
62 tableView->setDirection(ScrollView::Direction::VERTICAL);
63 tableView->setPosition(Vec2(winSize.width-150,winSize.height/2-120));
64 tableView->setDelegate(this);
65 tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN);
66 this->addChild(tableView);
67 tableView->reloadData();
68
69 return true;
70}
71
72void TableViewTest::tableCellTouched(TableView* table, TableViewCell* cell)
73{
74 CCLOG("cell touched at index: %ld", static_cast<long>(cell->getIdx()));
75}
76
77Size TableViewTest::tableCellSizeForIndex(TableView *table, ssize_t idx)
78{
79 if (idx == 2) {
80 return Size(100, 100);
81 }
82 return Size(60, 60);
83}
84
85TableViewCell* TableViewTest::tableCellAtIndex(TableView *table, ssize_t idx)
86{
87 auto string = StringUtils::format("%ld", static_cast<long>(idx));
88 TableViewCell *cell = table->dequeueCell();
89 if (!cell) {
90 cell = new (std::nothrow) CustomTableViewCell();
91 cell->autorelease();
92 auto sprite = Sprite::create("Images/Icon.png");
93 sprite->setAnchorPoint(Vec2::ZERO);
94 sprite->setPosition(Vec2(0, 0));
95 cell->addChild(sprite);
96
97 auto label = Label::createWithSystemFont(string, "Helvetica", 20.0);
98 label->setPosition(Vec2::ZERO);
99 label->setAnchorPoint(Vec2::ZERO);
100 label->setTag(123);
101 cell->addChild(label);
102 }
103 else
104 {
105 auto label = (Label*)cell->getChildByTag(123);
106 label->setString(string);
107 }
108
109
110 return cell;
111}
112
114{
115 return 20;
116}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
USING_NS_CC_EXT
virtual bool init() override
virtual ssize_t numberOfCellsInTableView(cocos2d::extension::TableView *table) override
virtual cocos2d::Size tableCellSizeForIndex(cocos2d::extension::TableView *table, ssize_t idx) override
virtual cocos2d::extension::TableViewCell * tableCellAtIndex(cocos2d::extension::TableView *table, ssize_t idx) override
virtual void tableCellTouched(cocos2d::extension::TableView *table, cocos2d::extension::TableViewCell *cell) override