如何在Corona中将行插入TableView?

时间:2015-02-13 15:48:25

标签: lua corona

我试图将匹配结果(string.find)渲染到一行,有点工作......但它只显示最后一个匹配。所以,如果我匹配' jan'和凯文'它会;只列出' kevin'

有没有办法解决这个问题?

代码:

local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}

for i=1,#MasterData.child do
    XMLdataTEST[i] = MasterData.child[i]
end

inputNumber = 1
check1 = 'jan'
check2 = 'kevin'

for i=1,#XMLdataTEST do
  local data1 = XMLdata[i].child[1].value
  local data2 = XMLdata[i].child[2].value
  local data3 = XMLdata[i].child[3].value
  local data4 = XMLdata[i].child[4].value

  input1 = string.lower( data1.. "" )
  input2 = string.lower(_G['check' .. inputNumber]  )
  input = input2

  if string.find( input1.. "" , input )  then
    print(inputNumber.. " match with " ..input)
    inputNumber = inputNumber + 1

    local function onRowRender( event )
      local row = event.row

      local number = display.newText(  row, "" .. row.index .. " - ", 12, 0, nil, 18 )
      number:setReferencePoint( display.CenterLeftReferencePoint )
      number.x = 15
      number.y = row.height * 0.5
      number:setFillColor( 0, 0, 0 )

      local name = display.newText(row, input1, 12, 0, nil, 18 )
      name:setReferencePoint( display.CenterLeftReferencePoint )
      name.x = number.x + number.contentWidth
      name.y = row.height * 0.5
      name:setFillColor( 0, 0, 0 )

      local score = display.newText(row,"testy", 12, 0, nil, 18 )
      score:setReferencePoint( display.CenterLeftReferencePoint )
      score.x = display.contentWidth - score.contentWidth - 20
      score.y = row.height * 0.5
      score:setFillColor( 0, 0, 0 )
    end

    local tableView = widget.newTableView
    {
       left = 0,
       top = 0,
       height = display.contentHeight,
       width = display.contentWidth,
      onRowRender = onRowRender,
        onRowTouch = onRowTouch,
      listener = scrollListener
    }
    tableView.x = display.contentWidth + display.contentWidth/2 + 50
    transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )

    for i = 1, 1 do
      local isCategory = false
      local rowHeight = 40
      local rowColor = { 255, 255, 255 }
      local lineColor = { 220, 220, 220 }

      tableView:insertRow
      {
          isCategory = isCategory,
          rowHeight = rowHeight,
          rowColor = rowColor,
          lineColor = lineColor,
          onRender = onRowRender,
      } 
    end
  end 
end

1 个答案:

答案 0 :(得分:2)

在外部循环中声明的tableView意味着它的第一个实例将由第二个实例转换。快速查看docs表示每个event.row允许可选的params表包含您渲染行所需的任何数据。

local MasterData = xml:loadFile( "sample.xml")
local XMLdataTEST = {}

for i=1,#MasterData.child do
    XMLdataTEST[i] = MasterData.child[i]
end

-- ** moved from loop **
local function onRowRender( event )
  local row = event.row

  local number = display.newText(  row, "" .. row.index .. " - ", 12, 0, nil, 18 )
  number:setReferencePoint( display.CenterLeftReferencePoint )
  number.x = 15
  number.y = row.height * 0.5
  number:setFillColor( 0, 0, 0 )

  -- ** changed to use params table **    
  local name = display.newText(row, row.params.input1, 12, 0, nil, 18 )
  name:setReferencePoint( display.CenterLeftReferencePoint )
  name.x = number.x + number.contentWidth
  name.y = row.height * 0.5
  name:setFillColor( 0, 0, 0 )

  local score = display.newText(row,"testy", 12, 0, nil, 18 )
  score:setReferencePoint( display.CenterLeftReferencePoint )
  score.x = display.contentWidth - score.contentWidth - 20
  score.y = row.height * 0.5
  score:setFillColor( 0, 0, 0 )
end

-- ** moved from loop **
local tableView = widget.newTableView {
       left = 0,
       top = 0,
       height = display.contentHeight,
       width = display.contentWidth,
       onRowRender = onRowRender,
       onRowTouch = onRowTouch,
       listener = scrollListener
    }
tableView.x = display.contentWidth + display.contentWidth/2 + 50
transition.to( tableView, { time=500, x=display.contentWidth / 2, transition=easing.inOutExpo } )

inputNumber = 1
check1 = 'jan'
check2 = 'kevin'

for i=1,#XMLdataTEST do
  local data1 = XMLdata[i].child[1].value
  local data2 = XMLdata[i].child[2].value
  local data3 = XMLdata[i].child[3].value
  local data4 = XMLdata[i].child[4].value

  input1 = string.lower( data1.. "" )
  input2 = string.lower(_G['check' .. inputNumber]  )
  input = input2

  if string.find( input1.. "" , input )  then
    print(inputNumber.. " match with " ..input)
    inputNumber = inputNumber + 1

    local isCategory = false
    local rowHeight = 40
    local rowColor = { 255, 255, 255 }
    local lineColor = { 220, 220, 220 }

    tableView:insertRow
    {
        isCategory = isCategory,
        rowHeight = rowHeight,
        rowColor = rowColor,
        lineColor = lineColor,
        -- ** pass input1 to onRowRender **
        params = { input1 = input1 }
    }
  end
end
相关问题