CCMultiColumnTableView added to CCTableView extension

Just did a quick port of the CCMultiColumnTableView subclass of CCTableView.

Seems to work well on top of CCTableView extension which was recently added to Cocos2d-x 2.0.2.

If you already have that CCTableView extension just copy CCMultiColumnTableView + CCTableView into you folder.

Enjoy!


CCMultiColumnTableView.zip (25.9 KB)

Cool, I will test it. Thanks. :slight_smile:

Could you write a test demo to ExtensionTest? And send us a Pull Request?

James Chen wrote:

Could you write a test demo to ExtensionTest? And send us a Pull Request?

I’m a github newbie….maybe you can add it to the repo?

So could you upload the test codes for CCMultiColumnTableView?

Herman Jakobi wrote:

James Chen wrote:
> Could you write a test demo to ExtensionTest? And send us a Pull Request?
>
I’m a github newbie….maybe you can add it to the repo?

@Herman Jakobi

Could u plz explain how to use it…thx a lot

Hi Herman,

Would you please share the usage of it?

Thanks a lot.

Herman Jakobi wrote:

Just did a quick port of the CCMultiColumnTableView subclass of CCTableView.
>
Seems to work well on top of CCTableView extension which was recently added to Cocos2d-x 2.0.2.
>
If you already have that CCTableView extension just copy CCMultiColumnTableView + CCTableView into you folder.
>
Enjoy!

Usage is basically the same like CCTableView. Check the interface and CCTableView usage

Hi Herman,

In fact, I already use it like CCTableView, but it seems the result is not correct. Since it is a very useful extension, why not provide a test case?

Anyway, I will try it again. If I still can not use it correctly, may I have you email?

Thx.

Herman Jakobi wrote:

Usage is basically the same like CCTableView. Check the interface and CCTableView usage

demilich husa wrote:

Hi Herman,
>
In fact, I already use it like CCTableView, but it seems the result is not correct. Since it is a very useful extension, why not provide a test case?
>
Anyway, I will try it again. If I still can not use it correctly, may I have you email?

Post (and describe) your problem here. Maybe some forum members can solve it, too. Currently I don’t have the time to give support for the code.
Btw since it’s a port of a Cocos2d extension - you will find the example on the net. Good luck

Hi Herman,

Following is my code. It almost same as CCTableView usage. The only difference is, I used:
pTableView~~>setColCount;
Other parts are totally same.

///////
h3. HelloWorldScene.h
#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H
#include “cocos2d.h”
#include “…/extensions/cocos-ext.h”
USING_NS_CC;
USING_NS_CC_EXT;
class HelloWorld : public CCLayer, public CCTableViewDataSource, public CCTableViewDelegate
{
public:
// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
virtual bool init;
// there’s no ‘id’ in cpp, so we recommand to return the exactly class pointer
static CCScene* scene;
virtual void scrollViewDidScroll;
virtual void scrollViewDidZoom;

virtual void tableCellTouched;
virtual CCSize cellSizeForTable;
virtual CCTableViewCell* tableCellAtIndex;
virtual unsigned int numberOfCellsInTableView;
CREATE_FUNC;
};
#endif // HELLOWORLD_SCENE_H

h3. HelloWorldScene.cpp
#include “HelloWorldScene.h”
#include “CCMultiColumnTableView.h”
CCScene* HelloWorld::scene
{
CCScene * scene = NULL;
do
{
// ‘scene’ is an autorelease object
scene = CCScene::create;
CC_BREAK_IF;
// ‘layer’ is an autorelease object
HelloWorld **layer = HelloWorld::create;
CC_BREAK_IF;
// add layer as a child to scene
scene~~>addChild;
} while ;
// return the scene
return scene;
}
// on “init” you need to initialize your instance
bool HelloWorld::init
{
bool bRet = false;
do
{
CC_BREAK_IF );
CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
CCMultiColumnTableView* pTableView = CCMultiColumnTableView::create);
pTableView~~>setDirection;
pTableView~~>setPosition;
pTableView~~>setDelegate;
pTableView~~>setVerticalFillOrder;
pTableView~~>reloadData;
pTableView~~>setColCount;
this~~>addChild;
bRet = true;
} while ;
return bRet;
}
void HelloWorld::tableCellTouched
{
CCLog);
}
CCSize HelloWorld::cellSizeForTable
{
return CCSizeMake;
}
CCTableViewCell* HelloWorld::tableCellAtIndex
{
CCString pString = CCString::createWithFormat;
CCTableViewCell
pCell = table~~>dequeueCell;
if
{
pCell = new CCTableViewCell;
pCell~~>autorelease;
CCSprite *pSprite = CCSprite::create;
pSprite~~>setAnchorPoint;
pSprite~~>setPosition;
pCell~~>addChild;

CCLabelTTF**pLabel = CCLabelTTF::create, “Arial”, 20.0);
pLabel~~>setPosition(CCPointZero);
pLabel~~>setAnchorPoint;
pLabel~~>setTag(123);
pCell~~>addChild;
}
else
{
CCLabelTTF *pLabel = pCell~~>getChildByTag(123);
pLabel~~>setString);
}
return pCell;
}
unsigned int HelloWorld::numberOfCellsInTableView
{
return 12;
}

void HelloWorld::scrollViewDidScroll
{
}
void HelloWorld::scrollViewDidZoom
{
}
Herman Jakobi wrote:

demilich husa wrote:
> Hi Herman,
>
> In fact, I already use it like CCTableView, but it seems the result is not correct. Since it is a very useful extension, why not provide a test case?
>
> Anyway, I will try it again. If I still can not use it correctly, may I have you email?
>
Post your problem here. Maybe some forum members can solve it, too. Currently I don’t have the time to give support for the code.
Btw since it’s a port of a Cocos2d extension~~ you will find the example on the net. Good luck

1 Like

Hi Herman,

Would you please give some hint?
For normal case, I can create the Tableview correctly. For multipleTableView, I think the only difference is need to setColCount. Am I correct?

If you can check above code or attached code, you are appreciated.

Thanks a lot.

Herman Jakobi wrote:

demilich husa wrote:
> Hi Herman,
>
> In fact, I already use it like CCTableView, but it seems the result is not correct. Since it is a very useful extension, why not provide a test case?
>
> Anyway, I will try it again. If I still can not use it correctly, may I have you email?
>
Post (and describe) your problem here. Maybe some forum members can solve it, too. Currently I don’t have the time to give support for the code.
Btw since it’s a port of a Cocos2d extension - you will find the example on the net. Good luck

I’m using cocos2d-2.1beta3-x-2.1.0,
and if you need to get this work, you need slight change in the cocos2dx extension source code.

Basically what Herman is doing is override ‘TableCell’ positioning stuffs,
and for that he did override
@
virtual unsigned int indexFromOffset;
virtual CCPoint
offsetFromIndex(unsigned int index);
virtual void _updateContentSize();
@

In Herman’s code those 3 functions does not have preceding underline(_) in function name
but in cocos2d-2.1beta3-x-2.1.0 those functions have preceding ‘_’. So change it.
And in original extension code, those 3 functions are not defined as ‘virtual’, so add ‘virtual’ keyword to those function declaration.

Now, everything is automatic.

in your old CCTableView codes, replace the creation of CCTableView to ‘CCMultiColumnTableView’.
CCMultiColumnTableView will calculate each TableCell’s position by checking each cell size via
@
CCSize cellSizeForTable(CCTableView *table)
@
and the number of cells via ‘numberOfCellsInTableView’.

I’m using cocos2d-2.1rc0-x-2.1.2.

extensions/GUI/CCScrollView/CCTableView.h

protected:
    // int __indexFromOffset(CCPoint offset);
    // CCPoint __offsetFromIndex(unsigned int index);

    virtual int __indexFromOffset(CCPoint offset);
    virtual CCPoint __offsetFromIndex(unsigned int index);

public:
    // void _updateContentSize();

    virtual void _updateContentSize();

extensions/GUI/CCScrollView/CCMultiColumnTableView.h

protected:
    // virtual int indexFromOffset(CCPoint offset);
    // virtual CCPoint offsetFromIndex(unsigned int index);
    // virtual void updateContentSize();

    virtual int __indexFromOffset(CCPoint offset);
    virtual CCPoint __offsetFromIndex(unsigned int index);
public:
    virtual void _updateContentSize();

extensions/GUI/CCScrollView/CCMultiColumnTableView.cpp

as well

I am now trying to adopt this extension to ver 2.1.4

By all means it is working with the fixes mentioned before as cocos2d-2.1rc0-x-2.1.2

But I have further checked that there is one major code difference in create function
table->_updateCellPositions(); found in the original CCTableView.

I try to add it in CCMultiColumnTableView, nothing affected

I just would like to make sure if anything need to work with this to confirm there is any more I need to check

m_vCellsPositions is affected which is mainly using in the overrided functions in CCTableView only

Did anyone manage to use this class?

Thanks!

Actually I didn’t work on it after the last message until now…
But I finally managed to use it with fixes on ver 2.2.0 (I am doing something dangerous taht I am not using the full version but just manual fixes, so it may not be that official)…

  1. This is the final part I fix that actually no matter what direction should reset the content size, otherwise the scrolling will follow the original one

    void CCMultiColumnTableView::setColCount(unsigned int cols)
    {
    m_colCount = cols;

     if (this->getDirection() == kCCScrollViewDirectionBoth) 
     {
         this->_updateCellPositions();
     }
    
     this->_updateContentSize();
    

    }

  2. And the sample code provided above has a critical error
    if (!pCell)
    This NULL checking is necessary but it is not in that way.
    … You just have to remember if it is NULL then create a new one, otherwise clear it and reuse it. If using the code above will make you confuse that the index will go wrong after a page, but seems OK if content fits in single page

  3. I have checked under Windows and Android device only. It is working but seems the performance may not be that good especially on scrolling.

I think I have to take some time to tidy up what I have fixed in the code and a little bit testing before I am confident to share the code. If someone request I may try to push it up, otherwise I will keep up with my pace. :slight_smile:

Hey Jonathan,

Can you push the updated code on Github or share it here? It would be great to use your extension.

Thanks,
Mircea

The whole folder of CCScrollView same as the attachment with this thread is attached. I believe you have to replace the file and do a diff to see what is changed

I just have a concern that since I am now working on a project, I just make manual replacement of some fixes in 2.1.4 with the fixes after (up to 2.2.0). Please do a checking after getting it into your project.

  1. I found a fix in CCScrollView after 2.1.4, I don’t think it is my fix but I found it is different from the files attached with this thread. Please pay attention if you have used CCScrollView in your project.

  2. I have added a NULL checking in several parts of CCTableView since I have to support a table with some cell is blank. Please pay attention to this if you need to make sure all cell is not blank
    Before: If the cell is EMPTY the program will crash
    After: The program will not crash when a cell is empty

I have done a checking in Windows and Android. Please let me know if you find weird stuff in iOS.

Thanks,
Jonathan

Just fixed one more part additionally that reloadData() has to be overrode as well

void CCMultiColumnTableView::reloadData(void)
{
    CCTableView::reloadData();

    if (this->getDirection() == kCCScrollViewDirectionBoth) 
    {
        this->_updateCellPositions();
    }

    this->_updateContentSize();
}