Skip to content
9miao Mobile Game edited this page Jun 24, 2014 · 3 revisions

CATableView Detailed Description

Class Description

CATableView is mainly used to generate list and display data in table. This one-dimensional table has either static or dynamic form and enable user to navigate through hierarchical data and realize many customizations via dataSource and delegate protocol. It’s quite efficient even dealing with mass data.

CATableView is allowed to only have one column of data (cell) and support longitudinal sliding. When the created tableView is first time displayed, we need to call reloadData method of it to force refresh one time, so as to update tableView data to its newest state.

Base Class

CAScrollView

Attribute

Access modifier

Attribute name

Description

Public

Frame

tableView Frame

Public

BackGround

Background image

Public

BackGroundScale9Image

Use 9 rectangle grids image as background

Private

TableHeaderView

Header view

Private

TableFooterView

Footer view

Protected

TableHeaderHeight

Header height

Protected

TableFooterHeight

Footer height

Protected

m_separatorColor

Separator line color

Protected

m_bAllowsSelection

Selectable or not

Protected

m_bAllowsMultipleSelection

Multi selectable or not

Method

Access modifier

Method name

Description

Public

initWithFrame

Initialize tableView and appoint Frame

Public

reloadData

Refresh tableView data

Public

setBackGroundImage

Set background image

Public

setBackGroundScale9Image

Use 9 rectangle grids image as background image

Attribute Description

Frame
Type: CCRect
Description: CAView shared attribute

BackGround
Type: CAImage*
Description: tableView background, get/sect {}

BackGroundScale9Image
Type: CAImage*
Description: tableView background, set by using 9 rectangle grids image, get/set {}

TableHeaderView
type:CAView*
Description: tableView header view, get/set {}

TableFooterView
Type:CAView*
Description: tableView footer view, get/set {}

TableHeaderHeight
Type:unsigned int
Description: tableView header height, get/set{}

TableFooterHeight
Type:unsigned int
Description:tableView footer height, get/set {}

m_separatorColor
Type:ccColor4B
Description:Separator line color, get/set {}

m_bAllowsSelection
Type:bool
Description: Allow or not to select cell, default value is false.

m_bAllowsMultipleSelection
Type:bool
Description:Allow or not to multi select, only allow to multi select when m_bAllowsSelection is true, default value is false, is/set {}

Method Description

virtual bool initWithFrame(const cocos2d::CCRect &rect)
Return value: bool
Description: shared Frame attribute of all CAView child class, it’s required to appoint Frame when creating tableView.

void reloadData()
Return value: void
Description: refresh tableView data.

tableView Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

void ThirdViewController::viewDidLoad()

{

         CCRect winRect = this->getView()->getBounds();

         

         tableView = new CATableView();

         tableView->setTableViewDelegate(this);

         tableView->setTableViewDataSource(this);

         tableView->initWithFrame(CCRect(winRect));

         tableView->setAllowsSelection(true);

         tableView->setBackGroundImage(CAImage::create("HelloWorld.png"));

         tableView->setSeparatorColor(ccc4(0,255,0,255));

         tableView->setAllowsMultipleSelection(true);

         this->getView()->addSubview(tableView);

         tableView->reloadData();

         tableView->release();

}

You must set agent CATableViewDelegate and CACATableViewDataSource when creating a tableView. Agent class contains all basic function ports of tableView and you need to inherit these two agents when using tableView, so we need to set tableView by methods in agent according to demands.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

void  ThirdViewController::tableViewDidSelectRowAtIndexPath(CATableView* table,

unsigned int section, unsigned int row)

{

         CCLog("selected = %d %d", section, row);

}

  

void ThirdViewController::tableViewDidDeselectRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row)

{

         CCLog("deselected = %d %d", section, row);

}

  

CATableViewCell* ThirdViewController::tableCellAtIndex(CATableView *table, unsigned int section, unsigned int row)

{

         CATableViewCell* cell = table->dequeueReusableCellWithIdentifier("aaa");

         if (cell == NULL)

         {

                   cell = CATableViewCell::create("aaa");

         }

  

         return cell;

}

  

CAView* ThirdViewController::tableViewSectionViewForHeaderInSection(CATableView* table, unsigned int section)

{

         CAView* view = CAView::createWithFrame(CCRect(0, 0, 0, 0), ccc4(200, 200, 255, 255));

  

         CCString* str = CCString::createWithFormat("Header - %u", section);

         CCLabelTTF* ttf = CCLabelTTF::create(str->getCString(), "Arial", 20);

         ttf->setColor(ccc3(127, 127, 127));

         ttf->setFrame(CCRect(10, 10, 0, 0));

         view->addSubview(ttf);

  

         return view;

}

  

CAView* ThirdViewController::tableViewSectionViewForFooterInSection(CATableView* table, unsigned int section)

{

         CAView* view = CAView::createWithFrame(CCRect(0, 0, 0, 0), ccc4(200, 255, 200, 255));

  

         CCString* str = CCString::createWithFormat("Footer - %u", section);

         CCLabelTTF* ttf = CCLabelTTF::create(str->getCString(), "Arial", 20);

         ttf->setColor(ccc3(127, 127, 127));

         ttf->setFrame(CCRect(10, 10, 0, 0));

         view->addSubview(ttf);

  

         return view;

}

unsigned int ThirdViewController::numberOfRowsInSection(CATableView *table, unsigned int section)

{

         return 3;

}

  

unsigned int ThirdViewController::numberOfSections(CATableView *table)

{

         return 3;

}

  

unsigned int ThirdViewController::tableViewHeightForRowAtIndexPath(CATableView* table, unsigned int section, unsigned int row)

{

         return 100;

}

  

unsigned int ThirdViewController::tableViewHeightForHeaderInSection(CATableView* table, unsigned int section)

{

         return 30;

}

  

unsigned int ThirdViewController::tableViewHeightForFooterInSection(CATableView* table, unsigned int section)

{

         return 30;

}

Operating effect:

Clone this wiki locally