Просмотр исходного кода

滑动列表添加刷新全部条目功能

薛鸿潇 5 лет назад
Родитель
Сommit
2becb80279
1 измененных файлов с 29 добавлено и 7 удалено
  1. 29 7
      assets/script/game/component/TableView.ts

+ 29 - 7
assets/script/game/component/TableView.ts

@@ -65,6 +65,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 +100,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 +163,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 +189,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);
     }