看了你的代码,主要是想优化 `switch_window_show` 函数中当 `id == 1` 时,表格内容重复生成的问题。根据浮生梦老师前端课程内容,优化思路是只创建一次表格项,后续通过更新数据来控制显示和状态。
核心优化点:
1. **首次创建**:在第一次打开时生成所有表格项并保存引用
2. **后续更新**:只更新数据,不重新创建控件
3. **红点管理**:通过保存的控件引用直接更新状态
以下是优化后的代码:
```lua
function npc:switch_window_show(id)
local JM_show = GUI:getChildByName(npc.ui.Image_1, string.format("Jm_%d", id))
npc.tujian_var = tujianshouji
if id == 1 then
GUI:setVisible(JM_show, true)
local pssj_ListView = GUI:getChildByName(JM_show, "pssj_ListView")
local item_show = GUI:getChildByName(JM_show, "sj_1_item")
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
-- 检查是否已经初始化过表格
if not npc._tableInitialized then
npc._tableItems = {} -- 保存所有物品控件
npc._tableWidgets = {} -- 保存所有父控件
for k, v in ipairs(npc.fbsj_t) do
local sj_show = GUI:getChildByName(pssj_ListView, string.format("sj_show_" .. k))
local sj_name = GUI:getChildByName(sj_show, "sj_name")
local sj_name_str = GUI:Text_getString(sj_name)
GUI:Text_setString(sj_name, string.format(sj_name_str, v.xilie))
local sj_sx = GUI:getChildByName(sj_show, "sj_sx")
GUI:Text_setString(sj_sx, v.str)
local x_js = v.mun == 6 and 98 or 70
local children = GUI:getChildren(sj_show)
-- 清理不需要的控件(只保留sj_name, sj_sx, sj_jd)
for i, child in ipairs(children) do
local name = GUI:getName(child)
if name ~= "sj_name" and name ~= "sj_sx" and name ~= "sj_jd" then
GUI:removeChildByName(sj_show, name)
end
end
-- 创建并保存所有物品项
local rowItems = {}
for kk, vv in ipairs(v) do
local item_show_clone = GUI:Clone(item_show)
GUI:setVisible(item_show_clone, true)
GUI:addChild(sj_show, item_show_clone)
GUI:setPosition(item_show_clone, 40 + x_js * (kk - 1), 50)
local shoulu = GUI:getChildByName(item_show_clone, "shoulu")
local itemData = {
index = SL:GetMetaValue("ITEM_INDEX_BY_NAME", vv),
look = true,
bgVisible = false,
count = 1,
color = 225
}
local item = GUI:ItemShow_Create(item_show_clone, "my_item_"..k.."_"..kk, 0, 0, itemData)
GUI:setPosition(item, current_opermode == 2 and 0 or 15, current_opermode == 2 and 0 or 15)
-- 保存控件引用
rowItems[kk] = {
widget = item_show_clone,
item = item,
shoulu = shoulu,
itemName = vv
}
-- 绑定点击事件
GUI:addOnClickEvent(shoulu, function()
SL:ScheduleOnce(function()
GUIUtil:Lua_SubmitForm("配饰图腾", "shoulututeng", k .. "#" .. kk)
npc:updateTableData() -- 更新表格数据
end, 0.5)
end)
end
npc._tableItems[k] = rowItems
npc._tableWidgets[k] = sj_show
end
npc._tableInitialized = true
-- 一键收录按钮事件
local Button_yjsl = GUI:getChildByName(JM_show, "Button_yjsl")
GUI:addOnClickEvent(Button_yjsl, function()
fangzhi_liandian(Button_yjsl, 0.3)
GUIUtil:Lua_SubmitForm("配饰图腾", "yijianshoulututeng", "")
SL:ScheduleOnce(function()
npc:updateTableData() -- 更新表格数据
end, 0.3)
end)
end
-- 更新表格数据
npc:updateTableData()
elseif id == 2 then
GUI:setVisible(JM_show, true)
local item_Panel_1 = GUI:getChildByName(JM_show, "item_Panel_1")
local ListView_fj = GUI:getChildByName(JM_show, "ListView_fj")
GUI:removeAllChildren(ListView_fj)
for i = 1, 10 do
local item_bj = GUI:Clone(item_Panel_1)
GUI:setVisible(item_bj, true)
GUI:addChild(ListView_fj, item_bj)
end
elseif id == 3 then
GUI:setVisible(JM_show, true)
elseif id == 4 then
GUI:setVisible(JM_show, true)
end
end
-- 新增:更新表格数据函数
function npc:updateTableData()
if not npc._tableItems then return end
npc.hd_mun = 0
local current_opermode = SL:GetMetaValue("CURRENT_OPERMODE")
for k, rowItems in ipairs(npc._tableItems) do
for kk, itemData in ipairs(rowItems) do
local widget = itemData.widget
local item = itemData.item
local shoulu = itemData.shoulu
local itemName = itemData.itemName
-- 检查是否已收录
if GetBagItemNumByName(self, itemName) >= 1 and npc.tujian_var.item_turn[itemName] ~= 1 then
-- 添加红点
local hd_but = GUI:getChildByName(widget, "img_hd")
if GUI:Win_IsNull(hd_but) then
local myImage = GUI:Image_Create(widget, "img_hd",
current_opermode == 1 and 28 or 43,
current_opermode == 1 and 28 or 43,
"res/public/btn_npcfh_04.png")
npc.hd_mun = npc.hd_mun + 1
end
GUI:ItemShow_setIconGrey(item, true)
else
-- 移除红点
GUI:removeChildByName(widget, "img_hd")
if npc.tujian_var.item_turn[itemName] == 1 then
GUI:ItemShow_setIconGrey(item, false)
local parent = GUI:getParent(widget)
GUI:removeChildByName(parent, "img_yishoulu")
local ysl = GUI:Image_Create(parent, "img_yishoulu",
current_opermode == 1 and 0 or 0,
current_opermode == 1 and -30 or -30,
"res/custom/PeiShiTuTeng/sl_1.png")
else
GUI:ItemShow_setIconGrey(item, true)
end
end
end
end
-- 更新总红点显示
GUI:setVisible(npc.hd_but, npc.hd_mun > 0)
-- 更新进度
npc:updateprogress()
end
```
**优化说明:**
1. **`_tableInitialized` 标志**:确保表格只创建一次
2. **`_tableItems` 缓存**:保存所有创建的控件引用,避免重复创建
3. **`updateTableData()` 函数**:专门负责更新数据,不涉及控件创建
4. **性能提升**:后续切换或更新时,只更新数据和状态,不重新创建控件
5. **内存优化**:减少临时对象的创建和销毁
**使用方法:**
- 首次打开界面时正常创建表格
- 后续切换或数据更新时,调用 `npc:updateTableData()` 即可
- 收录物品后会自动更新状态
这样优化后,你的表格就不会重复生成了,性能会得到明显提升!如果还有不明白的地方,随时问我哦~