creator 仿cocos2dx实现的的tableview组件(TypeScript)

先看看这个组件的滚动效果:


组件效果

tip:最后那个button按钮是为了看当前有多少个示例TableCellNode。
当前一共是显示100个cell的TableView,最后打印的话是实际复用了8个cell。

TableView组件源码:

const { ccclass, property } = cc._decorator;

export interface IDataSource {
    tableCellSizeForIndex(table: TableView, idx: number): cc.Size;

    tableCellAtIndex(table: TableView, idx: number): TableCellNode;

    numberOfCellsInTableView(table: TableView): number;
}

export interface IDelegate {
    tableCellTouched(table: TableView, cell: TableCellNode, touch: cc.Touch)
}

export class TableCellNode extends cc.Node {
    idx: number;//复用的时候会更改IDX
    realIdx: number;//用于标记第一次创建的时候的IDX

    constructor() {
        super();

        this.idx = -1;
        this.realIdx = -1;
    }
}

/**
 * 目前只考虑支持垂直方向的TableView
 */
@ccclass
export default class TableView extends cc.Component {
    scrollView: cc.ScrollView = null;
    dataSource: IDataSource = null;
    delegate: IDelegate = null;

    mCurYList: number[] = [];
    mCurCell: TableCellNode[] = [];
    mIdleCell: TableCellNode[] = [];

    mLastShowBeginI: number = 0;
    mLastShowEndI: number = 0;
    mLastTouch: cc.Touch = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        //由于我们的srcroolView是在onLoad的时候执行的,所以我们再onLoad之前都不能这样做
        this.scrollView = this.getComponent(cc.ScrollView);
        if (!this.scrollView)
            throw Error("need ScrollView Component");

        //用于记录点击的位置。 onScrollEvent的回调中并没有位置参数
        let event = new cc.Component.EventHandler();
        event.target = this.node;
        event.component = "TableView";
        event.handler = "onScrollEvent";
        this.scrollView.scrollEvents.push(event);
        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    start() {
        console.log("TableView start");
        this.reload();
    }

    // update (dt) {}

    setDataSource(dataSource: IDataSource) {
        this.dataSource = dataSource;
        console.log("TableView setDataSource");
        this.reload();
    }

    setDelegate(delegate: IDelegate) {
        this.delegate = delegate;
    }

    dequeueCell(): TableCellNode {
        if (this.mIdleCell.length > 0)
            return this.mIdleCell.pop();
        return null;
    }

    reloadNoMove() {
        let oldY = this.scrollView.content.y;//记录之前的位置。
        console.log("oldy=" + oldY);
        this.reload();
        this.scrollView.content.y = oldY;
        this.onChangeContentPos();
    }

    reload() {
        if (!cc.isValid(this.scrollView))
            return;

        if (this.dataSource) {
            //测量tableCell的高度,计算需要显示几个cell

            //渲染之前把之前使用的cell推入空闲cell中
            while (this.mCurCell.length > 0) {
                let idle = this.mCurCell.pop();
                // idle.removeFromParent();
                idle.active = false;
                // console.log("recycle 0 cell idx=" + idle.idx);
                this.mIdleCell.push(idle);//把最下面的cell推入空闲中
            }
            this.scrollView.content.y = this.scrollView.node.height / 2;//回退到最上面

            let len = this.dataSource.numberOfCellsInTableView(this);
            let viewPortH = this.node.height;
            this.mCurYList = [];
            let curH = 0;
            this.mLastShowBeginI = 0;
            this.mLastShowEndI = 0;
            // console.log("len=" + len);

            let i = 0;
            for (; i < len; i++) {
                let curCellH = this.dataSource.tableCellSizeForIndex(this, i).height;
                // console.log("curCellH=" + curCellH + ",viewPortH=" + viewPortH);
                if (curH < viewPortH)
                    this.mLastShowEndI = i;
                this.mCurYList.push(-curH);
                curH += curCellH;
            }
            // console.log("this.mLastShowBeginI=" + this.mLastShowBeginI + ",this.mLastShowEndI=" + this.mLastShowEndI);

            this.scrollView.content.height = curH;

            for (let i = this.mLastShowBeginI; i <= this.mLastShowEndI; i++) {
                let cell = this.dataSource.tableCellAtIndex(this, i);
                cell.idx = i;
                if (!cell.parent) {
                    this.scrollView.content.addChild(cell);
                }
                if (cell.realIdx == -1)
                    cell.realIdx = cell.idx;

                this.mCurCell.push(cell);
                cell.active = true;
                cell.y = this.mCurYList[i];
            }

            //排序是为了方便做回收处理,从上往下排列
            this.mCurCell.sort(function (a, b): number {
                return b.y - a.y;//从大到小排列
            });
        }
    }

    onTouchStart(event: cc.Event.EventTouch) {
        console.log("onTouchStart event x=" + event.touch.getLocationX() + ",y=" + event.touch.getLocationY());
        // let point = this.node.convertToNodeSpaceAR(cc.v2(event.touch.getLocationX(), event.touch.getLocationY()));
        // console.log("onTouchStart point x=" + point.x + ",y=" + point.y);
        // this.mLastClickY = point.y - this.scrollView.content.y;
        // console.log("onTouchStart y(origin,offset)=" + (point.y - this.node.height / 2) + "," + this.mLastClickY + ",this.scrollView.content.y=" + this.scrollView.content.y);
        this.mLastTouch = event.touch;
    }

    mScrolling: boolean = false;

    /**
     * 上边缘继续往下拖拽: SCROLL_BEGAN->SCROLLING->SCROLL_TO_TOP->SCROLLING->TOUCH_UP【放手】
     *                   ->BOUNCE_TOP->SCROLLING->AUTOSCROLL_ENDED_WITH_THRESHOLD->SCROLLING->SCROLL_ENDED
     *
     * 中间拖拽:SCROLL_BEGAN->SCROLLING->TOUCH_UP【放手】->SCROLL_ENDED
     * 下边缘继续往上拖拽: SCROLL_BEGAN->SCROLLING->SCROLL_TO_BOTTOM->SCROLLING->TOUCH_UP【放手】
     *                   ->BOUNCE_BOTTOM->SCROLLING->AUTOSCROLL_ENDED_WITH_THRESHOLD->SCROLLING->SCROLL_ENDED
     *
     * 点击: TOUCH_UP
     * @param scrollView
     * @param eventType
     * @param customEventData
     */
    onScrollEvent(scrollView: cc.ScrollView, eventType: cc.ScrollView.EventType, customEventData: string) {
        // console.log("scrolling=" + cc.ScrollView.EventType.SCROLLING);
        // console.log("onScrollEvent scrollview=" + scrollView + ",eventType=" + eventType + ",customEventData=" + customEventData + ",this.mScrolling=" + this.mScrolling);
        if (eventType == cc.ScrollView.EventType.SCROLL_BEGAN) {
            this.mScrolling = true;
        } else if (eventType == cc.ScrollView.EventType.SCROLL_ENDED) {
            this.mScrolling = false;
        } else if (eventType == cc.ScrollView.EventType.SCROLLING) {
            if (!this.dataSource) {
                return;
            }
            this.onChangeContentPos();
        } else if (eventType == cc.ScrollView.EventType.TOUCH_UP) {
            // console.log("this.delegate = " + this.delegate);
            if (!this.mScrolling && this.delegate && this.mLastTouch) {
                for (let i = 0; i < this.mCurCell.length; i++) {
                    let child = this.mCurCell[i];
                    let rect = child.getBoundingBoxToWorld();
                    // console.log("[" + i + "] rect=" + rect + ",point=" + this.mLastTouch.getLocation() + ",result=" + rect.contains(this.mLastTouch.getLocation()));
                    if (rect.contains(this.mLastTouch.getLocation())) {
                        this.delegate.tableCellTouched(this, child, this.mLastTouch);
                        break;
                    }
                }
            }
        }
    }

    onChangeContentPos() {
        //滚动的时候需要计算显示哪几个cell
        // console.log(scrollView.content.y);
        let viewY1 = this.scrollView.content.y - this.scrollView.node.height / 2;
        let viewY2 = viewY1 + this.node.height;
        let curY = 0;
        let showBeginI = -1;
        let showEndI = -1;
        for (let i = 0; i < this.mCurYList.length; i++) {
            let curCellH = this.dataSource.tableCellSizeForIndex(this, i).height;
            if (curY + curCellH < viewY1) {//视口上面
            } else if (curY > viewY2) {//视口下面
                break
            } else {//视口里面
                if (showBeginI == -1)
                    showBeginI = i;
                showEndI = i;
            }
            curY += curCellH;
        }
        if (showBeginI == -1 || showEndI == -1)
            return;
        if (showBeginI >= this.mLastShowBeginI && showEndI <= this.mLastShowEndI) {
            return;//显示的cell区间不变
        }

        let showNewBeginI = showBeginI;
        let showNewEndI = showEndI;
        if (showBeginI < this.mLastShowBeginI) {//手指往下滑动,组件往下滑动,导致当前显示的顶端cell索引小于之前的
            if (showEndI < this.mLastShowEndI) {
                let curEnd = this.mLastShowEndI;
                while (curEnd > showEndI) {
                    if (this.mCurCell.length > 0) {
                        let idle = this.mCurCell.pop();
                        //之前采用remove的方法导致子cell中添加的TOUCH_START监听后面都无效了,现在改成置为active为false
                        //来取代从父节点移除操作。其实也没必要移除因为后续又要添加进来,这样算起来还是切换active更高效点
                        // idle.removeFromParent();
                        idle.active = false;
                        this.mIdleCell.push(idle);//把最下面的cell推入空闲中
                        // console.log("recycle 1 cell idx=" + idle.idx);
                    }
                    curEnd--;
                }
            }
            showNewEndI = this.mLastShowBeginI - 1;
        } else if (showEndI > this.mLastShowEndI) {//手指往上滑动,组件往上滑动,导致当前显示底段的cell索引大于之前的
            if (showBeginI > this.mLastShowBeginI) {
                let curBegin = this.mLastShowBeginI;
                while (curBegin < showBeginI) {
                    if (this.mCurCell.length > 0) {
                        let idle = this.mCurCell.shift();
                        // idle.removeFromParent();
                        idle.active = false;
                        this.mIdleCell.push(idle);//把最下面的cell推入空闲中
                        // console.log("recycle 2 cell idx=" + idle.idx);
                    }
                    curBegin++;
                }
            }
            showNewBeginI = this.mLastShowEndI + 1;
        }
        // console.log("showBeginI=" + this.mLastShowBeginI + "," + showBeginI + ",showEndI=" + this.mLastShowEndI + "," + showEndI + " | showNewBeginI=" + showNewBeginI + ",showNewEndI=" + showNewEndI);
        this.mLastShowBeginI = showBeginI;
        this.mLastShowEndI = showEndI;
        for (let i = showNewBeginI; i <= showNewEndI; i++) {
            let cell = this.dataSource.tableCellAtIndex(this, i);
            cell.idx = i;//更新IDX
            if (!cell.parent) {
                this.scrollView.content.addChild(cell);
            }
            if (cell.realIdx == -1) {
                // console.log("add new cell idx=" + cell.idx);
                cell.realIdx = cell.idx;
            }
            this.mCurCell.push(cell);
            cell.active = true;
            cell.y = this.mCurYList[i];
        }
        //排序是为了方便做回收处理,从上往下排列
        this.mCurCell.sort(function (a, b): number {
            return b.y - a.y;//从大到小排列
        });
    }
}

接口IDataSource 用来TableView的表现。
接口IDelegate 用来回调某个cell的点击。

Demo下载地址

@注意Demo中的TableView没有及时更新,以本文中的TableView为准

实际问题1 文字叠影

文字叠影

如果设置没有背景图片,纯Label的时候,我们的TableView后面会有文字叠影的情况,修复的方法目前就是cell再加一个背景底,这样渲染出来的文字就正常了


文字叠影的Item

把我们的Item的背景图片改成不透明:

不透明
添加背景图片,文字叠影问题解决
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,542评论 6 504
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,822评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,912评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,449评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,500评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,370评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,193评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,074评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,505评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,722评论 3 335
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,841评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,569评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,168评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,783评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,918评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,962评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,781评论 2 354

推荐阅读更多精彩内容

  • 2017.02.22 可以练习,每当这个时候,脑袋就犯困,我这脑袋真是神奇呀,一说让你做事情,你就犯困,你可不要太...
    Carden阅读 1,345评论 0 1
  • 信任是爱的基础。 现实生活中,很多自认为忠于婚姻的人,为什么会陷入无休止的争吵呢?而争吵不会给他们的婚姻带来任何好...
    陈晓莲阅读 570评论 4 9
  • “今天终于开张了!”手里拎着六颗毛桃,转头听见刚买了水果的店老板娘兴高采烈地喊。也许她并没在意我还没走远,就已经按...
    时慧慧爱物阅读 115评论 0 1
  • 今天一大早就起来了,吃了牛肉米粉,就赶紧去车站赶火车,第一次坐12个小时火车,想想都害怕,让老公前一晚在超市给我买...
    苏苏日记阅读 328评论 2 1
  • 情系闹市莫家女 才冲九天云中鹤 不要人夸要颜色 自留清气满乾坤
    莫紫渔阅读 167评论 2 2