0

So I'm trying to fill a TableView with the values from a single field in an array of Custom Objects, but my TableView isn't being filled with the array I'm getting. This is my code:

function SearchWindow(){
    var self = Ti.UI.createWindow({
        backgroundColor:'#d0d0d0d'
    });
    var Cloud = require('ti.cloud');
    Cloud.debug=true;

    var searchBar = Ti.UI.createSearchBar({
        showCancel:false
    });
    var data = [];
    Cloud.Objects.query({
        classname:'Reservacion'
    }, function(e){
        if(e.success){
            for(var i=0; i< e.Reservacion.length; i++){
                data.push({title : e.Reservacion[i].nombre});
            }
        } else {
            alert('Error \n ' + ((e.error && e.message) || JSON.stringify(e)));
        }
    });

    var tableView = Ti.UI.createTableView({
        backgroundColor:'#d0d0d0',
        search:searchBar,
        data: data,
        filterAttribute : 'title'
    });

    self.add(tableView);
    return self;
}

I'm filling the data array with the value from a field called nombre from a Custom Object called Reservacion , but the data isn't being shown on the Table View. Is my approach wrong? If so, then how can I fill my Table View with the desired data?

Uriel Arvizu
  • 1,876
  • 6
  • 37
  • 97

1 Answers1

1

You are pushing the value of nombre field to the data array after creating the table view. This means when the table view is created, there are no rows, so that is why you are seeing nothing. Fix this by setting tableView.data = data; after data is fully populated (in your Cloud.Objects.query callback).

Instantiate your table view without specifying data:

var tableView = Ti.UI.createTableView({
    backgroundColor:'#d0d0d0',
    search:searchBar,
    filterAttribute:'title'
});

I hope it works.

Dawson Toth
  • 5,580
  • 2
  • 22
  • 37
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
  • no, the `TableView` is shown empty, I've put some alerts to check if the data array is being filled and it is. I have no idea as to why the TableView isn't filled with the array. – Uriel Arvizu Aug 14 '12 at 18:53
  • @UrielArvizu: is the table view is there ? just change the color of the tableview and see – Midhun MP Aug 14 '12 at 19:14
  • 1
    I found the solution: I need to add the array inside the callback function since it's asynchronous, so when I fill the Tableview with the array, it is not necessarily filled by the time the function is called. Here's the final result http://pastebin.com/kj5Xsw7h – Uriel Arvizu Aug 14 '12 at 19:58
  • @UrielArvizu: thanks for your comment. Sorry I couldn't help you. – Midhun MP Aug 14 '12 at 20:02
  • @MidhunMP: filterAttribute was a good idea. But that only specifies the property to use when filtering, not the filter to apply to the table view. The searchBar controls the actual filter. Might want to take a gander at the documentation and try it out for yourself: http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.UI.TableView-property-filterAttribute – Dawson Toth Aug 14 '12 at 21:52