for i=1,#_cfg do
-- Create Image_2
local Image_2 = GUI:Image_Create(ui.ListView_1, "Image_2", 0, 245, "res/custom/SuiPianDuiHuan/2.png")
GUI:setAnchorPoint(Image_2, 0.00, 0.00)
GUI:setTouchEnabled(Image_2, false)
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-04-18 17:51:38 |
👁️ 阅读: 8
你的代码中有几个问题需要修正:
1. **字符串分割错误**:`string.match(_cfg[i].idx, "#")` 这样使用无法正确分割字符串
2. **创建到ListView的方式不正确**:需要创建模板项然后添加到ListView中
根据浮生梦老师前端课程内容,正确的做法应该是:
```lua
for i=1,#_cfg do
-- 创建模板项
local template = GUI:Widget_Create(ui.ListView_1, "template_"..i, 0, 0, 500, 300)
-- 创建背景
local Image_2 = GUI:Image_Create(template, "Image_2", 0, 0, "res/custom/SuiPianDuiHuan/2.png")
GUI:setAnchorPoint(Image_2, 0.00, 0.00)
GUI:setTouchEnabled(Image_2, false)
GUI:setTag(Image_2, 0)
-- 创建装饰图片
local Image_3 = GUI:Image_Create(Image_2, "Image_3", 9, 6, "res/custom/SuiPianDuiHuan/3.png")
GUI:setAnchorPoint(Image_3, 0.00, 0.00)
GUI:setTouchEnabled(Image_3, false)
GUI:setTag(Image_3, 0)
-- 创建按钮
local Button_2 = GUI:Button_Create(Image_2, "Button_2", 447, 16, "res/custom/SuiPianDuiHuan/1.png")
GUI:Button_setTitleText(Button_2, [[]])
GUI:Button_setTitleColor(Button_2, "#ffffff")
GUI:Button_setTitleFontSize(Button_2, 16)
GUI:Button_titleEnableOutline(Button_2, "#000000", 1)
GUI:setAnchorPoint(Button_2, 0.00, 0.00)
GUI:setTouchEnabled(Button_2, true)
GUI:setTag(Button_2, 0)
-- 正确的字符串分割方式
local idxStr = tostring(_cfg[i].idx or "")
local duihuanwuStr = tostring(_cfg[i].duihuanwu or "")
local idxSplit = string.split(idxStr, "#")
local duihuanwuSplit = string.split(duihuanwuStr, "#")
local a = tonumber(idxSplit[1] or 0)
local b = tonumber(idxSplit[2] or 0)
local c = tonumber(duihuanwuSplit[1] or 0)
local d = tonumber(duihuanwuSplit[2] or 0)
-- 创建物品显示1
local ItemShow_1 = GUI:ItemShow_Create(Image_2, "ItemShow_1", 104, 31, {
index = a,
count = b,
look = true,
bgVisible = false,
color = 255
})
GUI:setAnchorPoint(ItemShow_1, 0.50, 0.50)
GUI:setTag(ItemShow_1, 0)
-- 创建物品显示2
local ItemShow_2 = GUI:ItemShow_Create(Image_2, "ItemShow_2", 384, 31, {
index = c,
count = d,
look = true,
bgVisible = false,
color = 255
})
GUI:setAnchorPoint(ItemShow_2, 0.50, 0.50)
GUI:setTag(ItemShow_2, 0)
-- 添加到ListView的正确方式
GUI:ListView_pushBackCustomItem(ui.ListView_1, template)
end
```
主要修正点:
1. 使用 `string.split()` 函数正确分割字符串
2. 创建模板Widget作为ListView的项
3. 使用 `GUI:ListView_pushBackCustomItem()` 将模板项添加到ListView中
4. 添加了类型转换和空值判断
这样就能正确创建ListView的每一项了。
← 返回问答库