如何在Corona中向TableView中插入行?

我试图将字符串匹配结果(string.find)呈现为一行,有些奇怪...但它只显示最后一次匹配。所以如果我匹配'jan'和'kevin',它只会列出'kevin'

有一种方法可以解决这个问题吗?

代码:

点赞
用户4402825
用户4402825

你在外部循环中声明的 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

-- ** 从循环中移除 **
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 )

  -- ** 修改以使用 params 表 **
  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

-- ** 从循环中移除 **
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,
        -- ** 将 input1 传递给 onRowRender **
        params = { input1 = input1 }
    }
  end
end
2015-02-17 05:19:58