TableView小部件中的文本不滚动

时间:2013-10-13 03:32:40

标签: lua widget tableview corona

我正在使用TableView小部件,它会创建一个单列表。我的onRowRender()函数与示例相同,但是当我运行我的应用程序时,每行的文本都卡在同一个位置,并且不随表一起移动。即使我将rowTitle.y属性的值更改为row.y(将文本放在正确的起始位置),当我滚动表时它也不会移动。

以下是我正在使用的示例代码,它来自Corona的文档,也在我的教科书中:

local widget = require( "widget" )

-- Listen for tableView events
local function tableViewListener( event )
    local phase = event.phase

    print( event.phase )
end

-- Handle row rendering
local function onRowRender( event )
    local phase = event.phase
    local row = event.row

    local rowTitle = display.newText( row, "Row " .. row.index, 0, 0, nil, 14 )
    rowTitle.x = row.x - ( row.contentWidth * 0.5 ) + ( rowTitle.contentWidth * 0.5 )
    rowTitle.y = row.contentHeight * 0.5
    rowTitle:setTextColor( 0, 0, 0 )
end

-- Handle row's becoming visible on screen
local function onRowUpdate( event )
    local row = event.row

    print( "Row:", row.index, " is now visible" )
end

-- Handle touches on the row
local function onRowTouch( event )
    local phase = event.phase

    if "press" == phase then
        print( "Touched row:", event.target.index )
    end
end

-- Create a tableView
local tableView = widget.newTableView
{
    top = 100,
    width = 320, 
    height = 510,
    maskFile = "mask-410.png",
    listener = tableViewListener,
    onRowRender = onRowRender,
    onRowTouch = onRowTouch,
}


-- Create 100 rows
for i = 1, 100 do
    local isCategory = false
    local rowHeight = 40
    local rowColor = 
    { 
        default = { 255, 255, 255 },
    }
    local lineColor = { 220, 220, 220 }

    -- Make some rows categories
    if i == 25 or i == 50 or i == 75 then
        isCategory = true
        rowHeight = 24
        rowColor = 
        { 
            default = { 150, 160, 180, 200 },
        }
    end

    -- Insert the row into the tableView
    tableView:insertRow
    {
        isCategory = isCategory,
        rowHeight = rowHeight,
        rowColor = rowColor,
        lineColor = lineColor,
    }
end 

-- delete the tenth row in the tableView
tableView:deleteRow( 10 )   

这是我的代码:

local widget = require "widget"
display.setStatusBar(display.HiddenStatusBar)
local availableSections = {"Intro", "Verse", "Chorus", "Verse", "Chorus", "Interlude", "Breakdown", "Verse", "Chorus", "Outro"}

--listen for tableView press events
local function tableViewListener( event )
    local phase = event.phase
    local row = event.target
    print(event.phase)
end

--render rows
local function onRowRender( event )
    local phase = event.phase
    local row = event.row

    local rowTitle = display.newText(availableSections[row.index], 0, 0, system.nativeFont, 24)
    rowTitle.x = row.x - (row.contentWidth * 0.5) + (rowTitle.contentWidth * 0.5) + 30
    rowTitle.y = row.contentHeight * 0.5
    rowTitle:setTextColor(1, 1, 1)
end

--handle row touches
local function onRowTouch( event )
    local phase = event.phase
    local row = event.target

    print("Row " .. row.index .. " touched.")

    if phase == "press" then
        print("Touched row:", event.target.index)
    end
end

-- Create a tableView
local list = widget.newTableView
{
    width = display.contentWidth, 
    height = display.contentHeight,
    --maskFile = "mask-410.png",
    listener = tableViewListener,
    onRowRender = onRowRender,
    onRowTouch = onRowTouch,
}

-- Create rows
for i = 1, table.getn(availableSections) do
    local isCategory = false
    local rowHeight = display.contentHeight/table.getn(availableSections)
    local rowColor = 
    { 
        default = { 255, 255, 255 },
    }
    local lineColor = { 220, 220, 220 }

    -- Insert the row into the tableView
    list:insertRow
    {
        isCategory = isCategory,
        rowHeight = rowHeight,
        rowColor = rowColor,
        lineColor = lineColor,
    }
end  

有什么我想念的吗?当表行可滚动时,为什么文本仍保留在原来的位置?

1 个答案:

答案 0 :(得分:2)

您必须将对象插入行组。

row:insert(rowTitle)
相关问题