var jobData = require('../data/job'); cc.Class({ extends: cc.Component, properties: { pageview: cc.PageView, jobItem: cc.Node, summaryLabel: cc.Label, artistIconSprite: cc.Sprite, artistLabel: cc.Label, jobIconFrames: [cc.SpriteFrame], }, bind(currentJobId, gender) { this.gender = gender; this.pageview.removeAllPages(); this.jobList = []; if (currentJobId) { for (var i = 0; i < jobData.length; i++) { if (jobData[i].id == currentJobId) { } else { this.jobList.push(jobData[i]); } } } else { this.jobList = jobData; } for (var i = 0; i < this.jobList.length; i++) { let item = cc.instantiate(this.jobItem); item.active = true; item.position = new cc.p(0, 0); item.getComponent('JobItem').bind(this.jobList[i]); this.pageview.addPage(item); } ///动态添加item的时候必须加这两行代码,刷新PageView的各种数值参数,让界面正常显示 this.pageview.sizeMode = cc.PageView.SizeMode.Free; this.pageview._updatePageView(); }, bindJobList(jobList) { this.pageview.removeAllPages(); this.jobList = jobList; for (var i = 0; i < this.jobList.length; i++) { let item = cc.instantiate(this.jobItem); item.active = true; item.position = new cc.p(0, 0); item.getComponent('JobItem').bind(this.jobList[i], this.gender); this.pageview.addPage(item); } ///动态添加item的时候必须加这两行代码,刷新PageView的各种数值参数,让界面正常显示 this.pageview.sizeMode = cc.PageView.SizeMode.Free; this.pageview._updatePageView(); }, bindSelectedJobText() { let jobInfo = this.jobList[this.pageview.getCurrentPageIndex()]; this.artistIconSprite.spriteFrame = this.jobIconFrames[jobInfo.id - 1]; this.artistLabel.string = jobInfo.name; this.summaryLabel.string = jobInfo.msg; }, getSelectedJob() { return this.jobList[this.pageview.getCurrentPageIndex()]; }, // update (dt) {}, });