实现CCTableView带滚动条(转)

转自:http://blog.sina.com.cn/s/blog_86ce3f9801019dd3.html


使用CCTableView可以实现拖动列表框功能,不过CCTableView没带滚动条,最近项目要求实现带滚动条,不停的尝试,基本可以满足要求了:

创建CCTableView,kCCScrollViewDirectionVertical纵向放置.

1
2
3
4
5
6
7
8
9
CCTableView* tableView = CCTableView::create( this , CCSizeMake(762, 439));
if (tableView != NULL)
{
     tableView->setDirection(kCCScrollViewDirectionVertical);
     tableView->setPosition(ccp(33, 30));
     tableView->setDelegate( this );
     tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
     this ->addChild(tableView);
}

 

滚动条:

1
2
3
4
5
6
7
8
CCSprite* bar = CCSprite::create( "ui/scrollBar.png" );
if (bar != NULL)
{
     bar->ignoreAnchorPointForPosition( false );
     bar->setAnchorPoint(ccp(0.5, 1));
     bar->setPosition(ccp(805, 465));
     this ->addChild(bar, 1, 1000);
}

 

滚动响应事件tableCellAtIndex:

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
CCTableViewCell* Fjut::tableCellAtIndex(CCTableView* table, unsigned int idx)
{
     CCString* string = CCString::createWithFormat( "%d" , idx);
   
     CCTableViewCell* cell = table->dequeueCell();
     if (cell == NULL)
     {
         cell = new CCTableViewCell();
         cell->autorelease();
 
         // test
         CCLabelTTF* label_ = CCLabelTTF::create(string->getCString(), "Aril" , 15);
         if (label_ != NULL)
         {
             label_->setPosition(ccp(65, 56));
             label_->setColor(FONT_COLOR_Green);
             cell->addChild(label_, 1, 100);
         }
     }
     else
     {
         scrollBar(table); // ...
 
         CCLabelTTF* label_ = (CCLabelTTF*)cell->getChildByTag(100);
         if (label_ != NULL)
         {
             label_->setString(string->getCString());
         }
     }
 
     return cell;
}

 

处理滚动:

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
void Fjut::scrollBar(CCTableView* table)
{
     CCNode* bar = this ->getChildByTag(1000);
     if (bar == NULL)
     {
         return ;
     }
 
     // tableSize.height == cell个数*cell的height
     CCSize tableSize = table->getContentSize();
     // CCTableView
     CCSize tableViewSize = table->getViewSize();
     // 每次拖动的偏移量?(负值)
     CCPoint contOffsetPos = table->getContentOffset();
 
     // 总的偏移量
     float maxOff = tableViewSize.height - tableSize.height;
     // 每次拖动的偏移量
     float curOff = contOffsetPos.y - maxOff;
     // 计算百分比
     float percentage = fabs (curOff)/ fabs (maxOff);
 
     printf ( "curOff:%f, maxOff:%f, per:%f\n" , curOff, maxOff, percentage);
     
     // 拖拉到最顶端或最低端后继续拖动(弹回)会出现percentage值小于0.1和大于1.0的情况,我们分别把percentage重置为0和1.0f
     if (percentage < 0.1f)
     {
         percentage = 0;
     }
     if (percentage > 1.0f)
     {
         percentage = 1.0f;
     }
 
     // bar移动到最顶端的position.y
     float barTopPosY = bar->getPosition().y;
     // bar移动到最低端的position.y
     float barLowPosY = 75.0f;
     // ....
     float h = barTopPosY - percentage*(barTopPosY- barLowPosY);;
 
     bar->setPosition(ccp(bar->getPosition().x, h));
}

 

当然,滚动条的长度要根据numberOfCellsInTableView中的cell个数进行缩放。相应的barLowPosY 也跟着变化。

如图:

QQ截图20130527215403

收工.


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值