You don't create new rows in the list, you create the number of rows you want to display and then re-use them with information from your source table as you scroll through it. If you want to change the table of information you want to display, you re-initialise the list using the other table.
A really quick and dirty example using 3 source tables.
The list only display 3 rows and each table has only 4 entries (so not much scrolling).
You can switch from one source table to another using /kl taunt, /kl hit, /kl stuff
Lua Code:
local initList = "taunt"
local sections = {
taunt = 1,
hit = 2,
stuff = 3,
}
local listEntries = {
[1]={
{text="123", value=1 },
{text="456", value=4 },
{text="789", value=7 },
{text="000", value=0 },
},
[2]={
{text="ABC", value=1 },
{text="DEF", value=2 },
{text="GHI", value=3 },
{text="JKL", value=4 },
},
[3]={
{text="MNO", value=5 },
{text="PWR", value=6 },
{text="STU", value=8 },
{text="VWX", value=9 },
},
}
local function UpdateList(self, id, init) -- the work of re-using the rows (buttons and texts) happens here
local table = listEntries[sections[id]]
if not table then return end
local numOptions = #table
if init then
self.offset = 0
end
local offset = self.offset or 0
local index, button
local entries = #self.rows
local current = 0
for i=1, #table do
index = offset + i
if table[index] then
current = current + 1
button = self.rows[current]
button:Show()
button.index = index
button.value = table[index].value
button.Text:SetText(table[index].text.." [value="..table[index].value.."]")
if current == entries then break end -- only loop enough to full #buttons
end
end
if current < entries then
for i = current + 1, entries do
self.rows[i]:Hide()
end
end
FauxScrollFrame_Update(self, numOptions, entries, self.rows[1]:GetHeight())
end
local function OnClick(self) -- Use the index set during UpdateList along with the curent source table (initList)
print("Clicked", listEntries[sections[initList]][self.index].value, listEntries[sections[initList]][self.index].text)
end
local f = CreateFrame("ScrollFrame", "KhazakList", UIParent, "FauxScrollFrameTemplate")
f:SetSize(100, 60)
f:SetPoint("TOPLEFT", 5, -5)
f.rows = {}
for i=1, 3 do -- create a fixed no. of rows to display (3)
local r = CreateFrame("Button", "$parentRow"..i, f)
tinsert(f.rows, r)
r:SetSize(f:GetWidth(), f:GetHeight()/3) -- The height of a row determines the relative offset the scroll bar uses (1/3 the list height here)
if i == 1 then
r:SetPoint("TOPLEFT")
else
r:SetPoint("TOP", f.rows[i-1], "BOTTOM")
end
f.Texture = f:CreateTexture()
f.Texture:SetAllPoints()
f.Texture:SetTexture("Interface/BUTTONS/WHITE8X8")
f.Texture:SetVertexColor(0.2, 0.2, 0.2, 0.5)
r.Text = r:CreateFontString()
r.Text:SetFontObject(GameFontNormal)
r.Text:SetPoint("LEFT", 4, 0)
r:SetScript("OnClick", OnClick)
end
f:SetScript("OnVerticalScroll", function(self, offset) -- When the scroll thumb moves, save the new offset and update from the current table
self.ScrollBar:SetValue(offset)
self.offset = math.floor(offset / self.rows[1]:GetHeight())
UpdateList(self, initList)
end)
UpdateList(f, initList, true) -- first time initalisation of the list.
_G["SLASH_KhazakList1"] = "/kl"
SlashCmdList.KhazakList = function(msg)
msg = strlower(msg)
if not sections[msg] then
print(msg, "not found in sections!")
return
end
initList = msg
UpdateList(f, initList, true) -- Initialise the list using the newly selected table
end