Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Delete all rows in a tableview with a single click

Hi there,

has anyone a good way to "clear" the table?
I want to build a button which deletes all my rows, so that i can add some new data in my tableview.

— asked August 3rd 2010 by Michael Gajda
  • button
  • clear
  • delete
  • iphone
  • mobile
  • table
  • tablerow
  • tableview
0 Comments

4 Answers

  • Accepted Answer

    var rd = [];
    tableView.data = rd;

    — answered August 3rd 2010 by Alan Bourke
    permalink
    0 Comments
  • When creating you TableView, you have check something:

    if you use tableView.setData() to populate you tableView, and you try to clear it with setData([]), it won't work!

    //This won´t work!
    //Clearing the tableView with tableView.setData([]) WON'T work!
    
    var data = [];
    for(var i = 0; i<10; i++)
    {
        var row = Ti.UI.createTableViewRow();
        data.push(row);
    }
    tableView.setData(data);
    
    //CLEARING THE TABLE
    //You will have to do something like this:
    if (tableView.data.length > 0) {
        for (var i = tableView.data[0].rows.length-1; i >= 0; i--) {
            tableView.deleteRow(i);
        }
    }
    

    If you use tableView.appendRow() to populate you tableView, then setData([]) will work just fine!

    //This will work!
    //Clearing the tableView with tableView.setData([]) WILL work!
    
    for(var i = 0; i<10; i++)
    {
        var row = Ti.UI.createTableViewRow();
        tableView.appendRow(row);
    }
    
    //CLEARING THE TABLE
    tableView.setData([]);
    
    — answered November 30th 2012 by Douglas Alves
    permalink
    0 Comments
  • the_table.setData([]);
    

    does not work ?

    — answered August 3rd 2010 by Dan Tamas
    permalink
    1 Comment
    • this works, too.

      $.resultTable.setData([]);
      

      — commented May 23rd 2014 by Nils Sdun
  • I tried it with that, but it has no effect to my Table,still all entrys are in it.

    — answered August 3rd 2010 by Michael Gajda
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.