Titanium Community Questions & Answer Archive

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

Remove a TableView from a function

I created a function:

var newsTopic = function()
{
   var tableview = Ti.UI.createTableView({
      data:data,
      top:68,
      backgroundColor: '#fff'
   });

   news.add(tableview);
}

and ran the function

newsTopic();

I have a button and when it's clicked I would like that table view to be removed.

I've tried:

btn.addEventListener('click', function()
{
   news.remove(tableview);
});

but when I run the app it crashes. I'm guess it's because the TableView is the in the function. How can I remove the TableView?

— asked August 17th 2010 by Chris Schultz
  • android
  • function
  • remove
  • tableview
0 Comments

1 Answer

  • Accepted Answer

    The scope of tableView it's private ( it's declared inside the function). So when you try to remove the tableview, there is nothing to remove.

    Take the var out of function

    
    var tableview;
    
    var newsTopic = function()
    {
       tableview = Ti.UI.createTableView({
    .....
    

    Untested but should work.

    — answered August 17th 2010 by Dan Tamas
    permalink
    2 Comments
    • Perfect. Thanks!

      — commented August 17th 2010 by Chris Schultz
    • how can I check if tableView exists in this case before removing?

      — commented March 25th 2012 by George Georgiou
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.