how to create a searchbar like in the weather app?

淺唱寂寞╮ 提交于 2019-12-11 18:18:34

问题


in my app I want to have a searchbar like in the weather app when you add a new city. I also want to search for citys/places with google geocoding and parse the results I already knkow how to do that works perfect. for this actually I have two questions.

first how do I create a searchbar that looks like the one in the weather app or the stokes app? a normal searchbar does not have a topic which i can work with. How did they do that ?

and second how can I become that instant/live search feeling? I mean that when you start typing it starts searching for what you typed in, kind a like for every letter you type it does a new search and presents data after a short while when you stopped typing. I cant do a query for every letter typed in, that would cause too much traffic and would take too long I guess.


回答1:


Create window based project, drop a view, drop a UISearchBar, drop a view on top of the search bar, make it rectangular, and you have a search bar with a topic. Then you have to implement the UISearchBarDelegate protocol so for each editing you look up the city names on a dictionary that you previously preloaded (or from internet directly) and perform a table insert if there is any matches. You can try to match the exact names, or use the levenshtein distance.

Probably you only want live results after a delay so do

[NSObject cancelPreviousPerformRequestsWithTarget:self];
[self performSelector:@selector(fillMyTableWithResults) withObject:nil afterDelay:1.0];

in the textDidChange method of the delegate.




回答2:


Have you looked at UISearchDisplayController. Take a look at this example.

EDIT Caching previous results is something that you can consider but without sending a request, you can end up showing stale data. While I haven't actually tried it out, I think the Three20 framework as it is built with the idea of web as a data source. It also has an equivalent TTSearchDisplayController. You can see if it helps.



来源:https://stackoverflow.com/questions/6068478/how-to-create-a-searchbar-like-in-the-weather-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!