cocos2d js 中TableView

var CustomTableViewCell = cc.TableViewCell.extend({
    draw: function (ctx) {
        this._super(ctx);
    }
});

var RankList = cc.Layer.extend({
    ctor: function () {
        this._super();
        this.init();
    },
    init: function () {
        var winSize = cc.director.getWinSize();

        var bg = new cc.Sprite(res.rankBg);
        bg.attr({
            x: winSize.width/2,
            y: winSize.height/2
        });
        this.addChild(bg);

        //创建一个tableview
        var tableView = new cc.TableView(this, cc.size(500, 100 * 4));

        //设置tableview的滑动的方向
        //cc.SCROLLVIEW_DIRECTION_HORIZONTAL 水平
        //cc.SCROLLVIEW_DIRECTION_VERTICAL 竖直
        tableView.setDirection(cc.SCROLLVIEW_DIRECTION_VERTICAL);
        tableView.x = 100;
        tableView.y = 200;

        // 设置委托
        tableView.setDelegate(this);

        //tableView填充方式  (cc.TABLEVIEW_FILL_BOTTOMUP)
        tableView.setVerticalFillOrder(cc.TABLEVIEW_FILL_TOPDOWN);
        this.addChild(tableView);

        //更新tableview
        tableView.reloadData();

        return true;
    },

    //TableView继承ScrollView有这俩个方法,不需要添加任何内容
    //scrollViewDidScroll: function (view) {
    //},
    //scrollViewDidZoom: function (view) {
    //},
    //设置点击cell后的回调函数
    tableCellTouched: function (table, cell) {
        cc.log("cell touched at index: " + cell.getIdx());
    },

    //设置cell大小
    tableCellSizeForIndex: function (table, idx) {
        return cc.size(100, 135);
    },
    //添加Cell
    tableCellAtIndex: function (table, idx) {
        var strValue = idx.toFixed(0);

        //获得一个cell,滑动cell的时候会执行这个方法,把没有显示(没渲染)的cell拿过来,更改内容,为了减小内存的开销

        var cell = table.dequeueCell();
        var label;
        if (!cell) {

            cell = new CustomTableViewCell();
            //添加图片
            var sprite = new cc.Sprite("res/1.png");
            sprite.anchorX = 0;
            sprite.anchorY = 0;
            sprite.x = 0;
            sprite.y = 0;
            cell.addChild(sprite);
            // 添加文本
            label = new cc.LabelTTF(strValue, "Helvetica", 50);
            label.x = 0;
            label.y = 0;
            label.anchorX = 0;
            label.anchorY = 0;
            label.color = cc.color(255, 0, 0, 255);
            label.tag = 123;
            cell.addChild(label);
        } else {
            //更改文本信息
            label = cell.getChildByTag(123);
            label.setString(strValue);
        }

        return cell;
    },
    //设置cell个数
    numberOfCellsInTableView: function (table) {
        return 10;
    }
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值