Keep Searchbar at the top of the Tableview when Scrolling
I would like to keep the searchbar visible at the top of a tableview when users scroll, is this possible? Right now as you scroll down through the items, the searchbar at the top scrolls with the items, so if i have a long list and scroll to the bottom and want to search, i have to scroll all the way back up to the top to get to the search bar.
4 Answers
-
Just if anyone still needs this, (as i could'nt find an answer to that)
You can set the searchHidden propertie of the TableView to true
and instead add the SearchBar directly to the TableView with the searchBars top propertie set to 0This works for me, however it seems to be a bit buggy, sometimes rows show duplicated and/or the transparent view the searchbar adds on focus is moved down for a row ( which is then clickable)
i also had many empty rows being displayed one time;anyway if someones going to give it a try
Maybe someone finds a way round these bugs
var win = Ti.UI.createWindow({ title:"Search Test" }) var table = Ti.UI.createTableView({}) var search = Ti.UI.createSearchBar({}) table.setData([{title:"A"},{title:"B"},{title:"C"},{title:"D"},{title:"E"},{title:"F"}]) table.searchHidden = true; table.search = search; search.top = 0; table.add(search); win.add(table) win.open()
-
you could add the search bar to the tableViewHeader and then it will always be at the top of the table
-
I think this actually works, not using the headerView but creating the search (or for my example here, the label) in the view and then positioning it above the tableview.
var headerLabel = Ti.UI.createLabel({ text:'Here is some text for the header label.', height:25, left:10, right:10 }); //Setup view header where the searchbar will below var tvHeader = Titanium.UI.createView({ width: '100%', height: 30, top: 0 }); tvHeader.add(headerLabel); win.add(tvHeader); //Setup tableview var tv = Titanium.UI.createTableView({ //headerView:tvHeader, search:search, filterAttribute: 'filter', top:30, width:WindowWidth }); win.add(tv);
-
I wouldn't want to open a new Question thread, as I have tried the above:
1- Inserting a SearchBar in a View, then apply that to the headerView of the TableView.
2- I tried playing with the top:0 of the searchBar.I cannot seem to find an example for Locking the search bar to the top of the TableView.
Here is what I have:
var tableData = []; var tableView = Ti.UI.createTableView({ top:0, left: 0, right: 0, bottom: 0, backgroundColor: '#2B0014', }); var searchBar = Titanium.UI.createSearchBar({ hintText: "Search by Country", barColor:'#000', showCancel:true, height:43, top:0, }); var Cloud = require('ti.cloud'); Cloud.Places.query({ page: 1, per_page: 140, order: 'name' }, function (e) { if (e.success) { //~~~ LIST the Places in a Table for(var i = 0; i < e.places.length; i++){ var place = e.places[i]; var row = Titanium.UI.createTableViewRow(dataObject: place); var label = Ti.UI.createLabel(); row.add(label); tableData.push(row); } tableView.data = tableData; tableView.search = searchBar; tableView.searchHidden = false; tableView.filterAttribute = 'country'; ...