Titanium Community Questions & Answer Archive

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

Adding Sections + Rows to TableView -- Doc examples don't work?

I'm looking to make a table view and then update it with data. if I put it in the "data" attribute it works, but at the time I initially load the tableView, I might not have the information.

Here's the code:

    var search = Titanium.UI.createSearchBar({});
    var profilesListWindow = Titanium.UI.createTableView({
        search: search,
        filterAttribute: "title",
        width: "65%",
        height: "80%",
        borderRadius: 20
    });

    backgroundSkin.add(profilesListWindow);

    var section = Titanium.UI.createTableViewSection();
    section.headerTitle = "Hello";
    var row1 = Titanium.UI.createTableViewRow({title:"Hello 1"});
    var row2 = Titanium.UI.createTableViewRow({title:"Hello 2"});
    section.add(row1);
    section.add(row2);
    profilesListWindow.add(section);

The last section is taken directly from the documentation and looks valid. It gives no errors, the tableView just doesn't show. I've tried moving when I show the table as well, thinking it was an ordering thing, and that didn't have any effect.

Thoughts on why this seemingly simple process won't work?

— asked May 2nd 2010 by Nathan Ziarek
  • tableview
  • tableviewrow
  • tableviewsection
0 Comments

2 Answers

  • Hey Nathan,

    You'll want to use the setData method if you want to add data to a tableView after it's been created. Try something like this:

    var search = Titanium.UI.createSearchBar({});
    var profilesListWindow = Titanium.UI.createTableView({
        search: search,
        filterAttribute: "title",
        width: "65%",
        height: "80%",
        borderRadius: 20
    });
    
    backgroundSkin.add(profilesListWindow);
    
    var data = [];
    var section = Titanium.UI.createTableViewSection();
    section.headerTitle = "Hello";
    var row1 = Titanium.UI.createTableViewRow({title:"Hello 1"});
    var row2 = Titanium.UI.createTableViewRow({title:"Hello 2"});
    section.add(row1);
    section.add(row2);
    data.push(section);
    
    profilesListWindow.setData(data);
    
    — answered May 2nd 2010 by Dan Giulvezan
    permalink
    3 Comments
    • True, but what if you want to keep the data that's still there? Won't setData replace everything in the table? So, as I understand it, if you want to add more rows to a table after it already has rows you have to use appendRow. Doing any of this with sectionView's I've had no luck so I've stayed away from them.

      — commented April 4th 2011 by Joe iEntry
    • Does anyone have a exampel when appendRow is working?

      — commented June 7th 2013 by Karl Svensson
    • I use appendRow all the time to dynamically add rows to a TableView. To do it with sections, call appendSection like this:

      MyTableView.appendSection(Ti.UI.createTableViewSection({headerView:MyNewSectionView}));
      

      then call appendRow().

      Sorry, I don't have a full simple example at the moment.

      — commented June 7th 2013 by Shawn Lipscomb
  • Hello,

    Try by changing this line:

    profilesListWindow.add(section);
    

    For this other line:

    profilesListWindow.appendSection(section);
    

    It should work.

    If not, keeping my firt suggestion, add this at the end of your code to refresh you table:

    var temp = $.profilesListWindow.data;
    $.profilesListWindow.setData(temp);
    

    I hope it helps.

    J. Pablo.

    — answered December 9th 2013 by Juan Pablo García Nieto
    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.