
原文请猛戳:
http://galoisplusplus.coding.me/blog/2014/09/20/gridview-in-cocos2d-x/
对于刚开始接触cocos2d-x的tableVIEw的人来说,这个UI类看上去并非顾名思义的是个table,而仅仅是个一维的List。
因为项目需要,我封装了一个tableVIEw的子类来实现二维的功能。
具体代码和测试代码详见:
cocos2d-x-GridView
<!--
(有时间的话我会解释为何要设计成继承tableVIEw和采用tableVIEw实现2D效果的workaround为何不适合项目需求)
-->
GrIDVIEw的使用与tableVIEw基本相同,只不过多了两个接口:
固定行数:调用setRowNum
固定列数:调用setColNum
这还需要修改一下cocos2d-x的tableVIEw类的声明,主要是声明一些member function为virtual:
class tableVIEw : public ScrollVIEw,public ScrollVIEwDelegate{public: enum class VerticalFillOrder { top_DOWN,BottOM_UP }; /** Empty contructor of tableVIEw */ static tableVIEw* create(); /** * An intialized table vIEw object * * @param dataSource data source * @param size vIEw size * @return table vIEw * @code * when this function bound to Js or lua,the input params are changed * in Js:var create(var JsObject,var size) * in lua:local create(var size) * in lua: * @endcode */ static tableVIEw* create(tableVIEwDataSource* dataSource,Size size); /** * An initialized table vIEw object * * @param dataSource data source; * @param size vIEw size * @param container parent object for cells * @return table vIEw * @code * when this function bound to Js or lua,var size,var container) * in lua:local create(var size,var container) * in lua: * @endcode */ static tableVIEw* create(tableVIEwDataSource* dataSource,Size size,Node *container); /** * @Js ctor */ tableVIEw(); /** * @Js NA * @lua NA */ virtual ~tableVIEw(); virtual bool initWithVIEwSize(Size size,Node* container = NulL); /** * data source * @Js NA * @lua NA */ virtual tableVIEwDataSource* getDataSource() { return _dataSource; } /** * when this function bound to Js or lua,the input params are changed * in Js:var setDataSource(var JsSource) * in lua:local setDataSource() * @endcode */ virtual voID setDataSource(tableVIEwDataSource* source) { _dataSource = source; } /** * delegate * @Js NA * @lua NA */ virtual tableVIEwDelegate* getDelegate() { return _tableVIEwDelegate; } /** * @code * when this function bound to Js or lua,the input params are changed * in Js:var setDelegate(var JsDelegate) * in lua:local setDelegate() * @endcode */ virtual voID setDelegate(tableVIEwDelegate* pDelegate) { _tableVIEwDelegate = pDelegate; } /** * determines how cell is ordered and filled in the vIEw. */ virtual voID setVerticalFillOrder(VerticalFillOrder order); virtual VerticalFillOrder getVerticalFillOrder(); /** * Updates the content of the cell at a given index. * * @param IDx index to find a cell */ virtual voID updateCellAtIndex(ssize_t IDx); /** * Inserts a new cell at a given index * * @param IDx location to insert */ virtual voID insertCellAtIndex(ssize_t IDx); /** * Removes a cell at a given index * * @param IDx index to find a cell */ virtual voID removeCellAtIndex(ssize_t IDx); /** * reloads data from data source. the vIEw will be refreshed. */ virtual voID reloadData(); /** * Dequeues a free cell if available. nil if not. * * @return free cell */ virtual tableVIEwCell *dequeueCell(); /** * Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query. * * @param IDx index * @return a cell at a given index */ virtual tableVIEwCell *cellAtIndex(ssize_t IDx); // OverrIDes virtual voID scrollVIEwDIDScroll(ScrollVIEw* vIEw) overrIDe; virtual voID scrollVIEwDIDZoom(ScrollVIEw* vIEw) overrIDe {} virtual bool ontouchBegan(touch *ptouch,Event *pEvent) overrIDe; virtual voID ontouchmoved(touch *ptouch,Event *pEvent) overrIDe; virtual voID ontouchended(touch *ptouch,Event *pEvent) overrIDe; virtual voID ontouchCancelled(touch *ptouch,Event *pEvent) overrIDe;protected: virtual long __indexFromOffset(Vec2 offset); virtual long _indexFromOffset(Vec2 offset); virtual Vec2 __offsetFromIndex(ssize_t index); virtual Vec2 _offsetFromIndex(ssize_t index); virtual voID _moveCellOutOfSight(tableVIEwCell *cell); virtual voID _setIndexForCell(ssize_t index,tableVIEwCell *cell); virtual voID _addCellifNecessary(tableVIEwCell * cell); virtual voID _updateCellpositions(); tableVIEwCell *_touchedCell; /** * vertical direction of cell filling */ VerticalFillOrder _vordering; /** * index set to query the indexes of the cells used. */ std::set<ssize_t>* _indices; /** * vector with all cell positions */ std::vector<float> _vCellspositions; //NSMutableIndexSet *indices_; /** * cells that are currently in the table */ Vector<tableVIEwCell*> _cellsUsed; /** * free List of cells */ Vector<tableVIEwCell*> _cellsFreed; /** * weak link to the data source object */ tableVIEwDataSource* _dataSource; /** * weak link to the delegate object */ tableVIEwDelegate* _tableVIEwDelegate; Direction _oldDirection; bool _isUsedCellsDirty;public: virtual voID _updateContentSize();}; 后记 虽然我当时折腾了一阵弄了GrIDVIEw,但后来我还是倾向于直接用tableVIEw实现二维列表的。比如在m行的tableVIEw的一行里放n个按钮就是mxn的列表,而按钮的响应可以是个c++lambda,有上下文信息,完全可以实现比较复杂的功能。
总结以上是内存溢出为你收集整理的cocos2d-x二维TableView全部内容,希望文章能够帮你解决cocos2d-x二维TableView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)