如何在 Corona SDK 的 tableView 中附加一些数据到行?

我有一个 tableView,我需要从不同来源向其中添加行。在使用原始的 widget 库时,我可以将数据附加到 insertRow 方法上,但是自从升级到2.0之后,似乎这是不可能的。

我认为应该能够附加数据的方式:

list:insertRow{
    rowHeight=rowHeight,
    isCategory=isCategory,
    rowColor=rowColor,
    lineColor=lineColor,
    data=data[i]
}

然后,data 应该可以在触摸事件中访问:

local function onRowTouch( event )
    print( event.target.data )
end

但是从widget2.0开始,情况并非如此。我的问题是:我该如何访问那些数据?

注意:在应用程序的其他部分中,我已经能够通过使用行索引作为表键来引用原始数据表,例如:print( data[event.row.index] ),但我无法在具有多个数据源的表上执行此操作。

点赞
用户271271
用户271271

我找到了一种不文档化的通过数据传递到表格行的方法。我将在这里分享,以防有人遇到相同的问题。由于这种方法没有文档,所以随时可能发生变化。

你可以使用以下方法附加数据: list._view._rows[#list._view._rows].data = data

data 可以是任何东西,并且它的工作方式与添加到 insertRow 方法相同。

要在touch方法中访问此数据,只需使用:event.target.data

2013-05-08 10:16:46
用户1870706
用户1870706

你使用行的索引值,并在 onRowRender 函数内引用数据表,这样就可以完成此操作:

local function onRowRender(event)
     id = event.index
     print(data[id])
end

list:insertRow{
    rowHeight=rowHeight,
    isCategory=isCategory,
    rowColor=rowColor,
    lineColor=lineColor,
}

通过索引值,你可以从数据表中获取任何你需要的数据。

2013-05-10 01:09:00