Cocos2d-x study notes (22) TableView

[Cocos2d-x] study notes directory

1 Introduction

TableView directly inherited ScrollView and ScrollViewDelegate. Description ScrollView TableView can achieve the effect of dragging the container. Rewrite the delegate methods scrollViewDidScroll ScrollViewDelegate, the function of ScrollView "expansion."

TableView ScrollView and can drag the visible range container, showing the container portion in the visible range, but the two are significantly different.

ScrollView all elements is shown to be applied to a disposable container, wherein the visual range of only selected portion of the display.

TableView is within the cell container "dynamic load", when the cell needs to appear in the visible range then added Container cell, leaving the cell put cell buffer visible range. TableView "dynamic loading" scrollViewDidScroll method requires ScrollView delegate real-time monitoring of changes in the visible range of the cell.

Any direction can ScrollView pan and zoom. TableView can drag and drag direction is limited to portrait or landscape.

2. TableViewCell

TableView the container is "dynamic loading" "dynamically removed." The so-called "dynamic" dependent on the basic unit cell container, the "load", "delete" refers to the operation of the cell.

TableViewCell directly inherited Node.

member variable IDX cell has, or is the index of cell number, used to identify the cell. idx -1 indicates that the cell is in an invalid state, and at this time there is no cell in the visible range of presentation, but the cache.

We are through the cell increases Sprite Label and other child nodes, so have the effect of cell images and text.

cell in descending order becomes a container.

3. TableViewDataSource TableViewDelegate

There TableView object Layer would have to be directly inherited TableViewDataSource and TableViewDelegate, if necessary can override these methods.

TableViewDataSource is for our custom class TableView data processing method, the method in this class we need to be rewritten.

Virtual Size tableCellSizeForIndex (the TableView * Table, an ssize_t idx); // The return idx of cell size 
Virtual Size cellSizeForTable (the TableView * Table); // return the cell size 
Virtual TableViewCell tableCellAtIndex * (* the TableView Table, an ssize_t idx) = 0 ; // The return idx cell 
Virtual an ssize_t numberOfCellsInTableView (the TableView Table *) = 0 ; // return the total number of cell

TableViewDelegate delegate class inherits directly ScrollViewDelegate.

virtual void tableCellTouched(TableView* table, TableViewCell* cell) = 0;
virtual void tableCellHighlight(TableView* table, TableViewCell* cell);
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell);
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell);

4. Some Properties

- TableViewCell *_touchedCell

Touch to use in the event three callback function, it refers to the current touch to the cell. We can deal with the current touch cell.

- Vertical Fill Order _vordering

Enumeration VerticalFillOrder comprising longitudinally aligned sequence from small to large TOP_DOWN, descending BOTTOM_UP.

- std::set<ssize_t>* _indices

Idx container visual, the visual range of the memory cell of idx.

- std::vector<float> _vCellsPositions

By _updateCellPositions method, we set according to cell size and the number of settings, each cell is calculated and stored starting x or y to the vessel. Finally, more than one position of the container, the length of the storage container.

- Vector<TableViewCell*> _cellsUsed

Visual container. Container storage cell in the visual area.

- Vector<TableViewCell*> _cellsFreed

Cache containers. Leaving the visible area of ​​the storage cell container, stored in the form of queue. Idx of the cell is set to -1, indicating an invalid cell.

- TableViewDataSource* _dataSource

- TableViewDelegate* _tableViewDelegate

Two delegate classes.

- Direction _oldDirection

And with the use _direction, NONE is set, and when the constructor reloadData method begins, for setting an initial position of container

- bool _isUsedCellsDirty

The dirty flag, containers are used to visible idx reordered. Will be set true if the number of cell changes in the visual container.

The main method

- create(TableViewDataSource* dataSource, Size size, Node *container)

Sets the default arrangement BOTTOM_UP, direction VERTICAL.

Call _updateCellPositions: Set the container _vCellsPositions.

Call _updateContentSize: Set container size, the length of the sum of all the cell; the container installation position.

- reloadData()

Reload TableView, equivalent to refresh TableView. Emptying the container and comprising a visual visual idx container, the container stored in the buffer cell, updating _vCellsPositions, container size and position re-set, execution state scrollViewDidScroll update the visual range cell.

After setting the new vertical arrangement order execution setVerticalFillOrder method, it will perform the method.

- cellAtIndex(ssize_t idx)

According idx return cell. idx idx and cell container must be visible and visual container that must be in the visible range cell, the cell is not returned invalid.

- scrollViewDidScroll(ScrollView* view)

The method for updating a state cell in the visible range.

The method provided in the container position setContentOffset, reloadData,

The main logic is:

1. Analyzing the dirty flag _isUsedCellsDirty. To true when the cell by visual container _cellsUsed idx from small to large. TableView the cell is ordered arrangement.

2. Calculate the offset. The container of the coordinate offset coordinates by -1, i.e. conversion "container position opposite the visible range" to "visible range of the relative position of container."

The reason is that the conversion is necessary to calculate the visible range idx cell after us, and cell are in order in the container. By visual range of the relative position of container, and it can be easily understood idx is calculated within the current cell in the visible range.

3. Calculate the smallest visible range idx cell (startIdx) and maximum idx (endIdx). positive cell within this range appear in the visible region.

4. The cross-border processing, i.e., "Delete" cell away from the visible range.

_CellsUsed visual container is not empty, the container has cell according idx from small to large, it is judged that the first and respectively the last cell starts from the container, determining relations or endIdx idx of the startIdx. If you need to "delete", is executed _moveCellOutOfSight method "delete", and continues to determine the next idx, cell idx until all the containers in the visible range.

The process of the new cell.

When no new cell, idx in the visible range and a storage cell idx container _indices visible clearly one correspondence. When there is a new cell, which is not visible in certain idx idx container _indices, updateCellAtIndex method needs to be performed, information of the cell is added to the vessel.

-  _moveCellOutOfSight(TableViewCell *cell)

This method is a cell away from the visible area for deletion and cache.

scrollViewDidScroll need to detect the current cell

The main logic is:

1. Call tableCellWillRecycle method of delegate. The delegate method

2. The following operating parameters of the cell:

  Was added to the buffer vessel _cellsFreed;

  _cellsUsed removed from the visual container;

  Remove it from the visual index idx container _indices;

  Which is set idx 1;

  container to remove the child node cell.

- _indexFromOffset(Vec2 offset)

The coordinates of the obtained cell index. The binary search _vCellsPositions vessel coordinates, determining the coordinates of the corresponding index.

- _offsetFromIndex(ssize_t index)

The cell number returns the coordinates. By correspondence relationship between a sequence number and _vCellsPositions returns coordinate values.

In _setIndexForCell method, when a cell setting position, the need to calculate the position by the method according to the index.

- updateCellAtIndex(ssize_t idx)

This method is used, the new information appears in the visible range of the updated cell container.

The only method to monitor scrollViewDidScroll method will be called when a new cell appears.

The main logic is:

1. idx generation parameters corresponding to the cell.

TableCellAtIndex method called dataSource, the method needs to be rewritten.

Cocos2dx official in the Demo, tableCellAtIndex main logic is: execution dequeueCell method. The first attempt to eject a cell from the cache container _cellsFreed in. If the cache container no cell, the new cell. If the pop-up cell, that cell as cell generation. Of course, whether or not the new cell, the index idx cell display parameters need to be changed idx.

2. Set idx and location of the cell. _SetIndexForCell method is performed.

3. Add the cell to the TableView: cell as a child node of the container. Add visual container cell _cellsUsed. Visualization was added idx idx vessel _indices. The dirty flag is set, the visual container for reordering.

6. Touch the event three callbacks

- onTouchBegan

When the presence of invisible parent node 1. TableView, returns false.

2. The method of performing onTouchBegan ScrollView parent class, storage Touch information, return value as the return value of the method TableView.

3. is a single touch, the touch point is calculated by which the coordinates of the current touch cell. If a cell exists, tableCellHighlight method of performing a delegate, and other operations may be performed highlight cell.

4. is a two-point touch and there is a touch cell (_touchedCell), the method performed tableCellUnhighlight delegate may cancel the highlighting of the cell. Then _touchedCell empty.

- onTouchMoved

A method for performing onTouchMoved ScrollView parent class, the drag on the container.

2. If there is _touchedCell drag, tableCellUnhighlight method of performing delegate, you can cancel the highlight of the cell. Then _touchedCell empty.

- onTouchEnded

1. If _touchedCell present, and the touch point in the visible range, the delegate performed tableCellTouched tableCellUnhighlight and the method ends.

Under normal circumstances, _touchedCell onTouchMoved the method should be left blank, while still present, a moving distance onTouchMoved too small.

2. Run the ScrollView onTouchEnded parent class.


This article addresses: https://www.cnblogs.com/deepcho/p/cocos2dx-tableview.html

Guess you like

Origin www.cnblogs.com/deepcho/p/cocos2dx-tableview.html