widget.newTableView()

类型 函数
widget.*
返回值 TableViewWidget
修订 版本 2024.3703
关键词 widget, tableview, 列表, listview
另请参阅 TableViewWidget

概述

创建一个 TableViewWidget 对象。

注意事项

语法

widget.newTableView( options )

此函数接受一个参数 options,它是一个包含以下参数的表

id (可选)

字符串要分配给表格视图的可选标识字符串。默认为 widget_tableView

x, y (可选)

数字控件的 **x** 和 **y** 中心点的坐标。如果定义了 lefttop 值,则这些值将被覆盖。

left, top (可选)

数字将在其中创建控件的左侧和顶部位置。如果指定,则这些值将覆盖 xy 参数。

width, height (可选)

数字表格视图的宽度和高度。

friction (可选)

数字确定向上或向下轻弹内容时的移动速度。默认值为 0.972

maxVelocity (可选)

数字限制表格视图的最大滚动速度。默认值为 2

isLocked (可选)

布尔值如果设置为 true,则表格视图将被锁定,这意味着它无法垂直滚动。

isBounceEnabled (可选)

布尔值如果设置为 false,则表格视图在到达上限或下限时将停止滚动。默认为 true

rowTouchDelay (可选)

数字指定传播行触摸事件之前的延迟(以毫秒为单位)。默认为 110 毫秒。

onRowRender (可选)

侦听器用于侦听 TableViewWidget 行渲染事件的函数。此函数在通过 object:insertRow() 添加行时、所有可见行重新渲染通过 object:reloadData()屏幕外行移回屏幕时调用。在此函数中,event.row 是对已渲染行的引用。

onRowTouch (可选)

侦听器用于侦听 TableViewWidget 触摸/点击事件的函数。在此侦听器函数中,event.phase 值包括 "tap""press""release""swipeLeft""swipeRight"。此外,event.target 是对与之交互的行的引用,event.target.index 是该行的索引号。

listener (可选)

侦听器用于侦听 TableViewWidget 触摸/滚动事件的函数。在此侦听器函数中,event.phase 返回预期的触摸交互阶段 "began""moved""ended"。此外,当表格视图轻弹/滑动动量自然停止时,可以检测到 "stopped" 阶段。最后,可以通过 event.limitReached 检测到任一方向的滚动限制。如果发生这种情况,event.direction 将根据表格视图到达限制时的移动方向返回 "up""down"

方法

object:insertRow()

object:reloadData()

object:deleteRow()

object:deleteRows()

object:deleteAllRows()

object:scrollToY()

object:scrollToIndex()

object:setIsLocked()

object:getContentPosition()

object:getNumRows()

object:getRowAtIndex()

视觉选项

可以使用 options 表中的以下参数自定义表格视图控件的视觉效果

noLines (可选)

布尔值如果设置为 true,则线条不会分隔各个行。默认值为 false

backgroundColor (可选)

表格包含 RGB+A 背景颜色设置的表格。默认为白色。

backgroundColor = { 0.8, 0.8, 0.8 }
hideBackground (可选)

布尔值如果为 true,则表格视图的背景将被隐藏,但它仍然可以接收触摸。

hideScrollBar (可选)

布尔值如果设置为 true,则表格视图将不会显示滚动条。

scrollBarOptions (可选)

包含自定义滚动条的 图像 表单 规格的表。自定义滚动条需要图像表单中的 3 帧。每帧都需要是大小相等的正方形。在 scrollBarOptions 表中,必须指定 4 个必需参数:sheettopFramemiddleFramebottomFrame

scrollBarOptions = {
    sheet = scrollBarSheet,  -- Reference to the image sheet
    topFrame = 1,            -- Number of the "top" frame
    middleFrame = 2,         -- Number of the "middle" frame
    bottomFrame = 3          -- Number of the "bottom" frame
}
topPadding, bottomPadding (可选)

数字这些数字表示内容到达相应限制时将停止的表格视图顶部和底部到像素的距离。两者的默认值均为 0

插入行

创建表格视图时,它只是创建一个指定宽度和高度的空白区域。实际行是通过 object:insertRow() 函数“渲染”的。

要完成此操作,需要进行一些基本设置。首先,编写一个目标侦听器函数来渲染新行,例如

local function onRowRender( event )

    -- Get reference to the row group
    local row = event.row

    -- Cache the row "contentWidth" and "contentHeight" because the row bounds can change as children objects are added
    local rowHeight = row.contentHeight
    local rowWidth = row.contentWidth

    local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )
    rowTitle:setFillColor( 0 )

    -- Align the label left and vertically centered
    rowTitle.anchorX = 0
    rowTitle.x = 0
    rowTitle.y = rowHeight * 0.5
end

接下来,在表格视图声明的 onRowRender 参数中指定该函数。此时,可以调用 object:insertRow() 插入新行,或者在循环中执行该调用以一次渲染多行。请参考下面的用法示例。

示例

基本的 TableView
local widget = require( "widget" )

-- The "onRowRender" function may go here (see example under "Inserting Rows", above)

-- Create the widget
local tableView = widget.newTableView(
    {
        left = 200,
        top = 200,
        height = 330,
        width = 300,
        onRowRender = onRowRender,
        onRowTouch = onRowTouch,
        listener = scrollListener
    }
)

-- Insert 40 rows
for i = 1, 40 do
    -- Insert a row into the tableView
    tableView:insertRow{}
end
带有类别行的 TableView
local widget = require( "widget" )

-- The "onRowRender" function may go here (see example under "Inserting Rows", above)

-- Create the widget
local tableView = widget.newTableView(
    {
        left = 200,
        top = 200,
        height = 330,
        width = 300,
        onRowRender = onRowRender,
        onRowTouch = onRowTouch,
        listener = scrollListener
    }
)

-- Insert 40 rows
for i = 1, 40 do

    local isCategory = false
    local rowHeight = 36
    local rowColor = { default={1,1,1}, over={1,0.5,0,0.2} }
    local lineColor = { 0.5, 0.5, 0.5 }

    -- Make some rows categories
    if ( i == 1 or i == 21 ) then
        isCategory = true
        rowHeight = 40
        rowColor = { default={0.8,0.8,0.8,0.8} }
        lineColor = { 1, 0, 0 }
    end

    -- Insert a row into the tableView
    tableView:insertRow(
        {
            isCategory = isCategory,
            rowHeight = rowHeight,
            rowColor = rowColor,
            lineColor = lineColor
        }
    )
end
带有自定义滚动条的 TableView
local widget = require( "widget" )

-- The "onRowRender" function may go here (see example under "Inserting Rows", above)

-- Create image sheet for custom scroll bar
local scrollBarOpt = {
    width = 20,
    height = 20,
    numFrames = 3,
    sheetContentWidth = 20,
    sheetContentHeight = 60
}
local scrollBarSheet = graphics.newImageSheet( "scrollBar.png", scrollBarOpt )

-- Create the widget
local tableView = widget.newTableView(
    {
        left = 200,
        top = 200,
        height = 330,
        width = 300,
        onRowRender = onRowRender,
        onRowTouch = onRowTouch,
        listener = scrollListener,
        scrollBarOptions = {
            sheet = scrollBarSheet,
            topFrame = 1,
            middleFrame = 2,
            bottomFrame = 3
        }
    }
)

-- Insert 40 rows
for i = 1, 40 do

    -- Insert a row into the tableView
    tableView:insertRow(
        {
            isCategory = false,
            rowHeight = 36,
            rowColor = { default={1,1,1}, over={1,0.5,0,0.2} },
            lineColor = { 0.5, 0.5, 0.5 }
        }
    )
end