Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing cells from scene2d.ui tables

Tags:

libgdx

I'm trying to remove a row from a table and have everything below that row move up one row. I am not succeeding at all. I've tried iterating through all the cells (with Table.getCells()) and updating them that way in various ways, but it just doesn't seem to work the way I intend to. Is there a way to do this?

like image 829
mattboy Avatar asked Feb 16 '23 11:02

mattboy


2 Answers

You can remove Actor with it cell like this:

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

It's not very handsome solution but it works

like image 145
Sergey Bubenshchikov Avatar answered Mar 20 '23 12:03

Sergey Bubenshchikov


A cleaner solution would be the next:

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));
     }
}
like image 30
JedyKnite Avatar answered Mar 20 '23 13:03

JedyKnite