|
|
@@ -4,17 +4,20 @@ const { ccclass, property } = cc._decorator;
|
|
|
/** 动态加载的滚动列表
|
|
|
* - 滑动列表动态加载
|
|
|
* - 支持多种类型预制体混合滚动
|
|
|
+ *
|
|
|
* - item脚本需要有函数 setItemData 用于接收数据
|
|
|
+ *
|
|
|
* - 多个prefab混合时,要为每条item指定 pfbType
|
|
|
+ *
|
|
|
* - item、content锚点y需要为1其他为0.5
|
|
|
* @author 薛鸿潇
|
|
|
*/
|
|
|
@ccclass
|
|
|
export default class TableView extends cc.Component {
|
|
|
|
|
|
- @property({ displayName: '预制条目', type: [cc.Prefab] })
|
|
|
+ @property({ displayName: '预制条目', tooltip: '脚本必须实现 setItemData 方法用于接收数据!', type: [cc.Prefab] })
|
|
|
private pre_item: cc.Prefab[] = [];
|
|
|
- @property({ displayName: '脚本名', type: [cc.String] })
|
|
|
+ @property({ displayName: '脚本名', tooltip: '脚本必须实现 setItemData 方法用于接收数据!', type: [cc.String] })
|
|
|
private scr_item: string[] = [];
|
|
|
/** 间距 */
|
|
|
@property({ displayName: '间距' })
|
|
|
@@ -65,6 +68,7 @@ export default class TableView extends cc.Component {
|
|
|
this.itemNode[i] = cc.instantiate(this.item[i]);
|
|
|
}
|
|
|
this.node.on('srollview-init', this.init, this);
|
|
|
+ this.node.on('srollview-update', this.updateList, this);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -99,11 +103,31 @@ export default class TableView extends cc.Component {
|
|
|
// cc.log('tableView结束:' + this.testTime());
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 刷新列表
|
|
|
- * 仅更新data数据
|
|
|
- */
|
|
|
- private updateList() {
|
|
|
+ /**
|
|
|
+ * 刷新列表
|
|
|
+ * - 仅支持使用1种预制体时,更新data数据
|
|
|
+ * @param itemData
|
|
|
+ */
|
|
|
+ public updateList(itemData: Array<any>) {
|
|
|
+ this.itemData = itemData;
|
|
|
+ const old_count = this.itemArr.length;
|
|
|
+ for (let i = 0; i < old_count; i++) {
|
|
|
+ if (this.itemData[i]) {
|
|
|
+ if (!this.itemArr[i]) {
|
|
|
+ let y = 0;
|
|
|
+ if (i > 0) {
|
|
|
+ y = this.itemArr[i - 1].y - this.itemArr[i - 1].height - this.itemInterval;// 下一条目纵坐标
|
|
|
+ }
|
|
|
+ let item_node = this.addItemNode(i, y);
|
|
|
+ this.updateContentHeigh(this.itemArr[i].height - item_node.y);
|
|
|
+ }
|
|
|
+ let comp_script = this.itemArr[i].getComponent(this.scr_item[this.itemData[i].pfbType || 0]);
|
|
|
+ comp_script && comp_script.setItemData(this.itemData[i], this);
|
|
|
+ } else {
|
|
|
+ this.itemArr[i].destroy();
|
|
|
+ this.itemArr[i] = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -142,8 +166,8 @@ export default class TableView extends cc.Component {
|
|
|
if (i > 0) {
|
|
|
y = this.itemArr[i - 1].y - this.itemArr[i - 1].height - this.itemInterval;// 下一条目纵坐标
|
|
|
}
|
|
|
- this.addItemNode(i, y);
|
|
|
- this.updateContentHeigh(this.itemArr[i].height - y);
|
|
|
+ let item_node = this.addItemNode(i, y);
|
|
|
+ this.updateContentHeigh(this.itemArr[i].height - item_node.y);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -168,6 +192,7 @@ export default class TableView extends cc.Component {
|
|
|
let comp_script = item.getComponent(this.scr_item[pfbType]);
|
|
|
comp_script && comp_script.setItemData(this.itemData[i], this);
|
|
|
this.itemArr.push(item);
|
|
|
+ return item;
|
|
|
// cc.log('生成itemNode' + i);
|
|
|
}
|
|
|
|