从scene2d.ui表中删除单元格

时间:2013-08-22 22:32:09

标签: libgdx

我正在尝试从表中删除一行,并让该行下方的所有内容向上移动一行。我根本没有成功。我已经尝试迭代所有单元格(使用Table.getCells())并以各种方式更新它们,但它似乎没有按照我打算的方式工作。有没有办法做到这一点?

4 个答案:

答案 0 :(得分:3)

您可以使用以下单元格删除Actor

public static void removeActor(Table container, Actor actor) {
    Cell cell = container.getCell(actor);
    actor.remove();
    // remove cell from table
    container.getCells().removeValue(cell, true);
    container.invalidate();
}

这不是很帅的解决方案,但它有效

答案 1 :(得分:2)

下一个更清洁的解决方案:

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));
     }
}

答案 2 :(得分:1)

有些睡眠解决了这个问题!下面的示例从具有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);

答案 3 :(得分:0)

在尝试了所有较早的答复之后,这对我来说效果很好。

    deleteStockButton.addListener(new ChangeListener() {
        public void changed(ChangeListener.ChangeEvent event, Actor actor) {
            if (stockTableIndex != null) {
                try {
                    Table stockTable = (Table) stockScroll.getActor();
                    List<Actor> cells = new ArrayList<>();
                    for (Cell c : stockTable.getCells().toArray(Cell.class)) {
                        cells.add(c.getActor());
                    }
                    cells.remove(stockTableIndex);

                    stockTable.clearChildren();

                    for (Actor a : cells) {
                        stockTable.row().pad(2);
                        stockTable.add(a).height(TEXT_HEIGHT).left().expandX();
                    }

                    stockTable.layout();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                stockTableIndex = null;
            }
        }
    });
相关问题