Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

Remove old tableview rows with every new search

$
0
0

I am running into an issue where every new search I run from my search bar adds new rows with the data in addition to rows with old search results. I am looking for every new search to remove all of the previous rows and data and create new rows based on the search results. I tried setting an empty data array at the start of the loop, but that doesnt work.

Attached is my code:

function findFriends(){
    var Cloud = require('ti.cloud');
 
    var findFriends = Ti.UI.createWindow({ title: 'Profile Settings',
            backgroundColor: '#fff',
            navTintColor : '#fff',
            translucent: false
 
            });
 
var data = [];
 
var searchBar = Ti.UI.createSearchBar({
    top:0,
    height:50,
    showCancel: true,
    hintText: "Search by username"
});
 
findFriends.add(searchBar); 
 
searchBar.addEventListener('return', function(){
    Cloud.Users.search({
    q: searchBar.value
   }, function (e) {
    if (e.success) {
 
        for (var i = 0; i < e.users.length; i++) {
            var user = e.users[i];
 
            var tableView = Ti.UI.createTableView({
                    top:0,
                    data: data,
                    search: searchBar
            });
 
            findFriends.add(tableView);
 
            tableView.setData([]);
 
            var row = Ti.UI.createTableViewRow({
                    height: 0,
                    id : user.id
            });
 
            var label = Ti.UI.createLabel({
                text: user.username,
            });
 
            row.add(label);
 
            });
 
            searchBar.addEventListener('cancel', function(){
    tableView.data = [];
});
 
        data.push(row);
       }
 
        tableView.setData(data);
 
    } else {
        alert('Error:\n' +
            ((e.error && e.message) || JSON.stringify(e)));
    }
});
 
 
 
});
 
 
 
 
    return findFriends;
}
 
module.exports = findFriends;

Viewing all articles
Browse latest Browse all 8068

Trending Articles