如何在 Corona 的 TableView 中插入行?

How to insert rows into TableView in Corona?

我正在尝试将匹配结果 (string.find) 呈现到一行中,有点效果......但它只显示最后一个匹配项。所以如果我匹配 'jan' 和 'kevin' 它会;仅列出 '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

您的 tableView 在外循环中声明意味着它的第一个实例将由第二个实例转换。快速查看 docs 表明每个 event.row 都允许可选的 params table 来包含呈现该行可能需要的任何数据。

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