I have a search bar that returns usernames to rows of relevant searches. When a search is made, the rows display the returned values, and when the search bar is cleared, the tableview is cleared, but when another search is made, the previous search results are displayed in the rows in addition to the new values appearing in the rows. I am looking to remove those previous search values when new searches are made. Below is my code:
function findFriends(){ var Cloud = require('ti.cloud'); Ti.App.Properties.getString('session_id'); Ti.API.info('session_id'); var findFriends = Ti.UI.createWindow({ title: 'Profile Settings', barColor:'#e67e22', 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); var row = Ti.UI.createTableViewRow({ height: 0, id : user.id }); var label = Ti.UI.createLabel({ text: user.username, }); row.add(label); var friendButton = Ti.UI.createButton({ title: '+Friend', backgroundColor: '#000', height: 25, width: 75, right: 5 }); row.add(friendButton); friendButton.addEventListener('click', function(){ Ti.App.Properties.getString('currentUser_id'); Cloud.Friends.add({ user_ids: row.id }, function (e) { if (e.success) { alert('Friend(s) added'); } else { alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e))); } }); }); 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;