概要
範例程式碼
建立 UI 表格
local frame = script.Parent
-- 表格數據
local headerWidth = { 200,\
80, 80 }
local headers = {
"名稱",
"工作",
"現金",
}
local\
data = {
{ "鮑勃", "服務生", 100 },
{ "麗莎", "警察", 200 },
{ "喬治", "-", 50 },
}
-- 首先,建立表格佈局
local uiTableLayout\
= Instance.new("UITableLayout")
uiTableLayout.FillDirection = Enum.FillDirection.Vertical
\
uiTableLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
uiTableLayout.VerticalAlignment\
= Enum.VerticalAlignment.Center
uiTableLayout.FillEmptySpaceColumns = false\
uiTableLayout.FillEmptySpaceRows = false
uiTableLayout.Padding = UDim2.new(0,\
5, 0, 5)
uiTableLayout.SortOrder = Enum.SortOrder.LayoutOrder
frame.Size =\
UDim2.new(0, 0, 0, 40) -- 父框的大小是單元格大小
uiTableLayout.Parent\
= frame
-- 接下來,創建列標頭
local headerFrame = Instance.new("\
框架")
headerFrame.Name = "標頭"
headerFrame.Parent = frame
for i =\
1, #headers do
local headerText = headers[i]
local headerCell = Instance.new("\
TextLabel")
headerCell.Text = headerText
headerCell.Name = headerText
\ headerCell.LayoutOrder = i
headerCell.Size = UDim2.new(0, 0, 0, 24)
headerCell.Parent\
= headerFrame
local headerSize = Instance.new("UISizeConstraint")
headerSize.MinSize\
= Vector2.new(headerWidth[i], 0)
headerSize.Parent = headerCell
end
\
-- 最後,通過迭代每行和該行中的列來添加數據行
for index, value in ipairs(data) do
local rowData = value
local rowFrame\
= Instance.new("Frame")
rowFrame.Name = "行" .. index
rowFrame.Parent\
= frame
for col = 1, #value do
local cellData = rowData[col]
local\
cell = Instance.new("TextLabel")
cell.Text = cellData
cell.Name\
= headers[col]
cell.TextXAlignment = Enum.TextXAlignment.Left
if tonumber(cellData)\
then -- 如果這個單元格是數字,則右對齊
cell.TextXAlignment\
= Enum.TextXAlignment.Right
end
cell.ClipsDescendants = true
\
cell.Size = UDim2.new(0, 0, 0, 24)
cell.Parent = rowFrame
end
endAPI 參考
屬性
FillEmptySpaceColumns
範例程式碼
建立 UI 表格
local frame = script.Parent
-- 表格數據
local headerWidth = { 200,\
80, 80 }
local headers = {
"名稱",
"工作",
"現金",
}
local\
data = {
{ "鮑勃", "服務生", 100 },
{ "麗莎", "警察", 200 },
{ "喬治", "-", 50 },
}
-- 首先,建立表格佈局
local uiTableLayout\
= Instance.new("UITableLayout")
uiTableLayout.FillDirection = Enum.FillDirection.Vertical
\
uiTableLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
uiTableLayout.VerticalAlignment\
= Enum.VerticalAlignment.Center
uiTableLayout.FillEmptySpaceColumns = false\
uiTableLayout.FillEmptySpaceRows = false
uiTableLayout.Padding = UDim2.new(0,\
5, 0, 5)
uiTableLayout.SortOrder = Enum.SortOrder.LayoutOrder
frame.Size =\
UDim2.new(0, 0, 0, 40) -- 父框的大小是單元格大小
uiTableLayout.Parent\
= frame
-- 接下來,創建列標頭
local headerFrame = Instance.new("\
框架")
headerFrame.Name = "標頭"
headerFrame.Parent = frame
for i =\
1, #headers do
local headerText = headers[i]
local headerCell = Instance.new("\
TextLabel")
headerCell.Text = headerText
headerCell.Name = headerText
\ headerCell.LayoutOrder = i
headerCell.Size = UDim2.new(0, 0, 0, 24)
headerCell.Parent\
= headerFrame
local headerSize = Instance.new("UISizeConstraint")
headerSize.MinSize\
= Vector2.new(headerWidth[i], 0)
headerSize.Parent = headerCell
end
\
-- 最後,通過迭代每行和該行中的列來添加數據行
for index, value in ipairs(data) do
local rowData = value
local rowFrame\
= Instance.new("Frame")
rowFrame.Name = "行" .. index
rowFrame.Parent\
= frame
for col = 1, #value do
local cellData = rowData[col]
local\
cell = Instance.new("TextLabel")
cell.Text = cellData
cell.Name\
= headers[col]
cell.TextXAlignment = Enum.TextXAlignment.Left
if tonumber(cellData)\
then -- 如果這個單元格是數字,則右對齊
cell.TextXAlignment\
= Enum.TextXAlignment.Right
end
cell.ClipsDescendants = true
\
cell.Size = UDim2.new(0, 0, 0, 24)
cell.Parent = rowFrame
end
endFillEmptySpaceRows
範例程式碼
建立 UI 表格
local frame = script.Parent
-- 表格數據
local headerWidth = { 200,\
80, 80 }
local headers = {
"名稱",
"工作",
"現金",
}
local\
data = {
{ "鮑勃", "服務生", 100 },
{ "麗莎", "警察", 200 },
{ "喬治", "-", 50 },
}
-- 首先,建立表格佈局
local uiTableLayout\
= Instance.new("UITableLayout")
uiTableLayout.FillDirection = Enum.FillDirection.Vertical
\
uiTableLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
uiTableLayout.VerticalAlignment\
= Enum.VerticalAlignment.Center
uiTableLayout.FillEmptySpaceColumns = false\
uiTableLayout.FillEmptySpaceRows = false
uiTableLayout.Padding = UDim2.new(0,\
5, 0, 5)
uiTableLayout.SortOrder = Enum.SortOrder.LayoutOrder
frame.Size =\
UDim2.new(0, 0, 0, 40) -- 父框的大小是單元格大小
uiTableLayout.Parent\
= frame
-- 接下來,創建列標頭
local headerFrame = Instance.new("\
框架")
headerFrame.Name = "標頭"
headerFrame.Parent = frame
for i =\
1, #headers do
local headerText = headers[i]
local headerCell = Instance.new("\
TextLabel")
headerCell.Text = headerText
headerCell.Name = headerText
\ headerCell.LayoutOrder = i
headerCell.Size = UDim2.new(0, 0, 0, 24)
headerCell.Parent\
= headerFrame
local headerSize = Instance.new("UISizeConstraint")
headerSize.MinSize\
= Vector2.new(headerWidth[i], 0)
headerSize.Parent = headerCell
end
\
-- 最後,通過迭代每行和該行中的列來添加數據行
for index, value in ipairs(data) do
local rowData = value
local rowFrame\
= Instance.new("Frame")
rowFrame.Name = "行" .. index
rowFrame.Parent\
= frame
for col = 1, #value do
local cellData = rowData[col]
local\
cell = Instance.new("TextLabel")
cell.Text = cellData
cell.Name\
= headers[col]
cell.TextXAlignment = Enum.TextXAlignment.Left
if tonumber(cellData)\
then -- 如果這個單元格是數字,則右對齊
cell.TextXAlignment\
= Enum.TextXAlignment.Right
end
cell.ClipsDescendants = true
\
cell.Size = UDim2.new(0, 0, 0, 24)
cell.Parent = rowFrame
end
endMajorAxis
範例程式碼
建立 UI 表格
local frame = script.Parent
-- 表格數據
local headerWidth = { 200,\
80, 80 }
local headers = {
"名稱",
"工作",
"現金",
}
local\
data = {
{ "鮑勃", "服務生", 100 },
{ "麗莎", "警察", 200 },
{ "喬治", "-", 50 },
}
-- 首先,建立表格佈局
local uiTableLayout\
= Instance.new("UITableLayout")
uiTableLayout.FillDirection = Enum.FillDirection.Vertical
\
uiTableLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
uiTableLayout.VerticalAlignment\
= Enum.VerticalAlignment.Center
uiTableLayout.FillEmptySpaceColumns = false\
uiTableLayout.FillEmptySpaceRows = false
uiTableLayout.Padding = UDim2.new(0,\
5, 0, 5)
uiTableLayout.SortOrder = Enum.SortOrder.LayoutOrder
frame.Size =\
UDim2.new(0, 0, 0, 40) -- 父框的大小是單元格大小
uiTableLayout.Parent\
= frame
-- 接下來,創建列標頭
local headerFrame = Instance.new("\
框架")
headerFrame.Name = "標頭"
headerFrame.Parent = frame
for i =\
1, #headers do
local headerText = headers[i]
local headerCell = Instance.new("\
TextLabel")
headerCell.Text = headerText
headerCell.Name = headerText
\ headerCell.LayoutOrder = i
headerCell.Size = UDim2.new(0, 0, 0, 24)
headerCell.Parent\
= headerFrame
local headerSize = Instance.new("UISizeConstraint")
headerSize.MinSize\
= Vector2.new(headerWidth[i], 0)
headerSize.Parent = headerCell
end
\
-- 最後,通過迭代每行和該行中的列來添加數據行
for index, value in ipairs(data) do
local rowData = value
local rowFrame\
= Instance.new("Frame")
rowFrame.Name = "行" .. index
rowFrame.Parent\
= frame
for col = 1, #value do
local cellData = rowData[col]
local\
cell = Instance.new("TextLabel")
cell.Text = cellData
cell.Name\
= headers[col]
cell.TextXAlignment = Enum.TextXAlignment.Left
if tonumber(cellData)\
then -- 如果這個單元格是數字,則右對齊
cell.TextXAlignment\
= Enum.TextXAlignment.Right
end
cell.ClipsDescendants = true
\
cell.Size = UDim2.new(0, 0, 0, 24)
cell.Parent = rowFrame
end
endPadding
範例程式碼
建立 UI 表格
local frame = script.Parent
-- 表格數據
local headerWidth = { 200,\
80, 80 }
local headers = {
"名稱",
"工作",
"現金",
}
local\
data = {
{ "鮑勃", "服務生", 100 },
{ "麗莎", "警察", 200 },
{ "喬治", "-", 50 },
}
-- 首先,建立表格佈局
local uiTableLayout\
= Instance.new("UITableLayout")
uiTableLayout.FillDirection = Enum.FillDirection.Vertical
\
uiTableLayout.HorizontalAlignment = Enum.HorizontalAlignment.Center
uiTableLayout.VerticalAlignment\
= Enum.VerticalAlignment.Center
uiTableLayout.FillEmptySpaceColumns = false\
uiTableLayout.FillEmptySpaceRows = false
uiTableLayout.Padding = UDim2.new(0,\
5, 0, 5)
uiTableLayout.SortOrder = Enum.SortOrder.LayoutOrder
frame.Size =\
UDim2.new(0, 0, 0, 40) -- 父框的大小是單元格大小
uiTableLayout.Parent\
= frame
-- 接下來,創建列標頭
local headerFrame = Instance.new("\
框架")
headerFrame.Name = "標頭"
headerFrame.Parent = frame
for i =\
1, #headers do
local headerText = headers[i]
local headerCell = Instance.new("\
TextLabel")
headerCell.Text = headerText
headerCell.Name = headerText
\ headerCell.LayoutOrder = i
headerCell.Size = UDim2.new(0, 0, 0, 24)
headerCell.Parent\
= headerFrame
local headerSize = Instance.new("UISizeConstraint")
headerSize.MinSize\
= Vector2.new(headerWidth[i], 0)
headerSize.Parent = headerCell
end
\
-- 最後,通過迭代每行和該行中的列來添加數據行
for index, value in ipairs(data) do
local rowData = value
local rowFrame\
= Instance.new("Frame")
rowFrame.Name = "行" .. index
rowFrame.Parent\
= frame
for col = 1, #value do
local cellData = rowData[col]
local\
cell = Instance.new("TextLabel")
cell.Text = cellData
cell.Name\
= headers[col]
cell.TextXAlignment = Enum.TextXAlignment.Left
if tonumber(cellData)\
then -- 如果這個單元格是數字,則右對齊
cell.TextXAlignment\
= Enum.TextXAlignment.Right
end
cell.ClipsDescendants = true
\
cell.Size = UDim2.new(0, 0, 0, 24)
cell.Parent = rowFrame
end
end