1

我正在使用一个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  

有什么我想念的吗?为什么表格行可滚动时文本会保留在原处?

4

1 回答 1

2

您必须将对象插入行组。

row:insert(rowTitle)
于 2013-10-14T00:47:59.130 回答