creator 仿cocos2dx實(shí)現(xiàn)的的tableview組件(TypeScript)

先看看這個(gè)組件的滾動(dòng)效果:


組件效果

tip:最后那個(gè)button按鈕是為了看當(dāng)前有多少個(gè)示例TableCellNode咆蒿。
當(dāng)前一共是顯示100個(gè)cell的TableView尉尾,最后打印的話是實(shí)際復(fù)用了8個(gè)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;//復(fù)用的時(shí)候會(huì)更改IDX
    realIdx: number;//用于標(biāo)記第一次創(chuàng)建的時(shí)候的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的時(shí)候執(zhí)行的萌庆,所以我們?cè)賝nLoad之前都不能這樣做
        this.scrollView = this.getComponent(cc.ScrollView);
        if (!this.scrollView)
            throw Error("need ScrollView Component");

        //用于記錄點(diǎn)擊的位置溶褪。 onScrollEvent的回調(diào)中并沒(méi)有位置參數(shù)
        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) {
            //測(cè)量tableCell的高度,計(jì)算需要顯示幾個(gè)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;

    /**
     * 上邊緣繼續(xù)往下拖拽: 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
     * 下邊緣繼續(xù)往上拖拽: SCROLL_BEGAN->SCROLLING->SCROLL_TO_BOTTOM->SCROLLING->TOUCH_UP【放手】
     *                   ->BOUNCE_BOTTOM->SCROLLING->AUTOSCROLL_ENDED_WITH_THRESHOLD->SCROLLING->SCROLL_ENDED
     *
     * 點(diǎn)擊: 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() {
        //滾動(dòng)的時(shí)候需要計(jì)算顯示哪幾個(gè)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區(qū)間不變
        }

        let showNewBeginI = showBeginI;
        let showNewEndI = showEndI;
        if (showBeginI < this.mLastShowBeginI) {//手指往下滑動(dòng)猿妈,組件往下滑動(dòng),導(dǎo)致當(dāng)前顯示的頂端cell索引小于之前的
            if (showEndI < this.mLastShowEndI) {
                let curEnd = this.mLastShowEndI;
                while (curEnd > showEndI) {
                    if (this.mCurCell.length > 0) {
                        let idle = this.mCurCell.pop();
                        //之前采用remove的方法導(dǎo)致子cell中添加的TOUCH_START監(jiān)聽(tīng)后面都無(wú)效了巍虫,現(xiàn)在改成置為active為false
                        //來(lái)取代從父節(jié)點(diǎn)移除操作彭则。其實(shí)也沒(méi)必要移除因?yàn)楹罄m(xù)又要添加進(jìn)來(lái),這樣算起來(lái)還是切換active更高效點(diǎn)
                        // 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) {//手指往上滑動(dòng)占遥,組件往上滑動(dòng),導(dǎo)致當(dāng)前顯示底段的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 用來(lái)TableView的表現(xiàn)。
接口IDelegate 用來(lái)回調(diào)某個(gè)cell的點(diǎn)擊瓦胎。

Demo下載地址

@注意Demo中的TableView沒(méi)有及時(shí)更新芬萍,以本文中的TableView為準(zhǔn)

實(shí)際問(wèn)題1 文字疊影

文字疊影

如果設(shè)置沒(méi)有背景圖片,純Label的時(shí)候搔啊,我們的TableView后面會(huì)有文字疊影的情況柬祠,修復(fù)的方法目前就是cell再加一個(gè)背景底,這樣渲染出來(lái)的文字就正常了


文字疊影的Item

把我們的Item的背景圖片改成不透明:

不透明
添加背景圖片负芋,文字疊影問(wèn)題解決
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末漫蛔,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌莽龟,老刑警劉巖蠕嫁,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異轧房,居然都是意外死亡拌阴,警方通過(guò)查閱死者的電腦和手機(jī)绍绘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門奶镶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人陪拘,你說(shuō)我怎么就攤上這事厂镇。” “怎么了左刽?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵捺信,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我欠痴,道長(zhǎng)迄靠,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任喇辽,我火速辦了婚禮掌挚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘菩咨。我一直安慰自己吠式,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布抽米。 她就那樣靜靜地躺著特占,像睡著了一般。 火紅的嫁衣襯著肌膚如雪云茸。 梳的紋絲不亂的頭發(fā)上是目,一...
    開(kāi)封第一講書(shū)人閱讀 52,268評(píng)論 1 309
  • 那天,我揣著相機(jī)與錄音标捺,去河邊找鬼胖笛。 笑死,一個(gè)胖子當(dāng)著我的面吹牛宜岛,可吹牛的內(nèi)容都是我干的长踊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼萍倡,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼身弊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤阱佛,失蹤者是張志新(化名)和其女友劉穎帖汞,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體凑术,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡翩蘸,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了淮逊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片催首。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖泄鹏,靈堂內(nèi)的尸體忽然破棺而出郎任,到底是詐尸還是另有隱情,我是刑警寧澤备籽,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布舶治,位于F島的核電站,受9級(jí)特大地震影響车猬,放射性物質(zhì)發(fā)生泄漏霉猛。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一珠闰、第九天 我趴在偏房一處隱蔽的房頂上張望惜浅。 院中可真熱鬧,春花似錦铸磅、人聲如沸赡矢。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)吹散。三九已至,卻和暖如春八酒,著一層夾襖步出監(jiān)牢的瞬間空民,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工羞迷, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留界轩,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓衔瓮,卻偏偏與公主長(zhǎng)得像浊猾,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子热鞍,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 2017.02.22 可以練習(xí)葫慎,每當(dāng)這個(gè)時(shí)候衔彻,腦袋就犯困,我這腦袋真是神奇呀偷办,一說(shuō)讓你做事情艰额,你就犯困,你可不要太...
    Carden閱讀 1,348評(píng)論 0 1
  • 信任是愛(ài)的基礎(chǔ)椒涯。 現(xiàn)實(shí)生活中柄沮,很多自認(rèn)為忠于婚姻的人,為什么會(huì)陷入無(wú)休止的爭(zhēng)吵呢废岂?而爭(zhēng)吵不會(huì)給他們的婚姻帶來(lái)任何好...
    陳曉蓮閱讀 572評(píng)論 4 9
  • “今天終于開(kāi)張了祖搓!”手里拎著六顆毛桃,轉(zhuǎn)頭聽(tīng)見(jiàn)剛買了水果的店老板娘興高采烈地喊泪喊。也許她并沒(méi)在意我還沒(méi)走遠(yuǎn)棕硫,就已經(jīng)按...
    時(shí)慧慧愛(ài)物閱讀 116評(píng)論 0 1
  • 今天一大早就起來(lái)了髓涯,吃了牛肉米粉袒啼,就趕緊去車站趕火車,第一次坐12個(gè)小時(shí)火車纬纪,想想都害怕蚓再,讓老公前一晚在超市給我買...
    蘇蘇日記閱讀 330評(píng)論 2 1
  • 情系鬧市莫家女 才沖九天云中鶴 不要人夸要顏色 自留清氣滿乾坤
    莫紫漁閱讀 190評(píng)論 2 2