2
我試圖從表中刪除一行,並將該行下面的所有內容都向上移動一行。我根本沒有成功。我已經嘗試遍歷所有單元格(使用Table.getCells()
)並以各種方式更新它們,但它似乎並不按照我想要的方式工作。有沒有辦法做到這一點?從scene2d.ui表中刪除單元格
我試圖從表中刪除一行,並將該行下面的所有內容都向上移動一行。我根本沒有成功。我已經嘗試遍歷所有單元格(使用Table.getCells()
)並以各種方式更新它們,但它似乎並不按照我想要的方式工作。有沒有辦法做到這一點?從scene2d.ui表中刪除單元格
有些睡眠解決了問題!下面的示例從具有2列的表中移除第一行,並將所有其他行向上移動一步。
List<Cell> cells = table.getCells();
//Remove contents of first row
cells.get(0).setWidget(null);
cells.get(1).setWidget(null);
//Copy all cells up one row
for (int i=0;i<cells.size()-2;i++){
cells.set(i, cells.get(i+2));
}
//Remove the last row
cells.remove(cells.size()-1);
cells.remove(cells.size()-1);
一個清潔的解決方案將是下一個:
public void removeTableRow(int row) {
SnapshotArray<Actor> children = table.getChildren();
children.ordered = false;
for (int i = row*COLUMN_NUMBER; i < children.size - COLUMN_NUMBER; i++) {
children.swap(i, i + COLUMN_NUMBER);
}
// Remove last row
for(int i = 0 ; i < COLUMN_NUMBER; i++) {
table.removeActor(children.get(children.size - 1));
}
}
你嘗試通過Table.layout()或Table.invalidate()去除細胞/行後更新表? – noone
是的,這對我當時正在做的事沒有什麼影響,但我現在就開始工作了。 – mattboy