我在表视图中使用UISearchDisplayController。它在数组中完美搜索。但是,当我不得不从Web服务中获取数据时该怎么办?

最佳答案

实现用于API交互的类。
如何:


http://www.raywenderlich.com/51127/nsurlsession-tutorial-使用默认的URLSession
要么
http://code.tutsplus.com/tutorials/working-with-nsurlsession-afnetworking-20--mobile-22651-第三方库。 (更易于首次使用)

在您的UITableViewController中,实现要发送请求的searchbar委托事件。例如:

      - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;



用这种方法调用您的API请求。


在从请求中回叫解析数据
如何:


https://www.safaribooksonline.com/blog/2013/03/06/parsing-json-in-objective-c-using-nsjsonserialization/-JSON
要么
http://www.raywenderlich.com/553/xml-tutorial-for-ios-how-to-choose-the-best-xml-parser-for-your-iphone-project-XML

然后根据解析的数据设置新的数据源数组,然后调用[tableView reloadData]。


附言:如果不清楚。给我发邮件,我将在此处发布示例项目。

关于ios - iOS uisearchviewcontroller用于Web服务,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34229430/

10-10 20:47
查看更多