看板 MacDev 關於我們 聯絡資訊
語法是Swift 目的是:在一個地圖MapView (UIViewController) 上放一個有search(display)controller處理的searchbar iOS8新的機制UISearchController下 我在mapview的controller可以宣告一個新UISearchController 指定他的searchResultsController與searchResultsDelegate 為一個我另外寫的UITableViewController Class 在那裡面處理search result的事件跟資料 藉由SearchController本身的searchBar變數就可把搜尋框加到MapView (也就是UIViewController) 開始在上面打字 畫面就會有切換到table view的動畫 原本的uivew會被這個tableview蓋掉 一切都很完美 code重點如下 (在主UIViewController中) let src = AutoCompleteController() //AutoCompleteController是 //TableViewController ...... searcher = UISearchController(searchResultsController: src) searcher.searchResultsUpdater = src searcher.searchBar.delegate = self searchBarView.addSubview(searcher.searchBar) searcher.searchBar.sizeToFit() //searchBar的delegate是在UIViewController上 //ResultsUpdater跟ResultsController都是另外拉的AutoCompleteController //並且相關的tableview處理動作也寫好在AutoComplet.... 然後小弟想要做IOS7的backward compatibility 所以回頭研究如何用UISearchDisplayController去實作一樣的功能 (我是SWIFT新手IOS8才開始學的XD) 但是感覺沒那麼簡單 先參考http://www.raywenderlich.com/76519/add-table-view-search-swift 的範例 然後在UIViewController也是一樣宣告一個跟範例一樣的TableViewController Class 在這裡叫src2 但是是實作UISearchDisplayController功能的 main UIViewController裡換成這樣 let searchBar = UISearchBar() searchBar.delegate = self let searcher2 = UISearchDisplayController(searchBar: searchBar, contentsController: src2) searcher2.searchResultsDataSource = src2 searcher2.searchResultsDelegate = src2 searchBarView.addSubview(searcher2.searchBar) searcher2.searchBar.sizeToFit() searchDisplayController應該設的DataSource跟Delegate都有給src2 但是在這個searchBar上打字 動畫跟tableview都沒有出來過 沒有動靜 不知道是不是我使用SearchDisplayController的觀念有問題 因為網路上教學的範例幾乎都是searchBar在TableViewController裏的 並沒有searchBar在普通的UIView裏 然後開始輸入就會出現tableview蓋掉畫面的 我覺得ios8 SearchController只是被我剛好瞎矇試成功的 架構兩者好像不太一樣 我相信是做得到的 因為ios版的google map 跟很多app都有看到這種效果 (searchBar放在非tableview的uiview中 點了才有搜尋結果tableview動畫) 希望有前輩們可以指點 感恩感恩 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.163.208.119 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1430749250.A.97A.html
tentenlee: code請放git 這樣看超辛苦... 05/05 13:35
purmac: 我用swift寫了一個demo放在github 應該是你想要的效果 05/05 14:49
purmac: 有什麼問題在發問 05/05 14:50
goodwise: t大抱歉 searchDisplayController失敗的沒放 05/06 12:04
goodwise: 這是ios8 SearchController的demo也分享給大家 05/06 12:05
goodwise: 有包括google place api的auto complete效果 05/06 12:05
goodwise: p大感激啊 我再試試 很可能只是stc.delegate沒加而已 05/06 12:06
goodwise: sbdc 更正 05/06 12:08
goodwise: to p大 我將searchBar放在另個有constraint的uiview 05/06 12:43
goodwise: uiview小區域再為main view的subview 05/06 12:44
goodwise: 依照您的方式加了以後 搜尋點了後indexPath.row有印 05/06 12:45
goodwise: 但是畫面沒有彈出tableview 05/06 12:45
goodwise: 截圖 tinyurl.com/mujkbtb 05/06 12:46
goodwise: ios8新版search controller我也是這樣加 效果就有 05/06 12:49
goodwise: code: tinyurl.com/na2n7xd 05/06 12:49
goodwise: stortyboard樹狀:tinyurl.com/n98h53d 05/06 12:51
goodwise: 跟google map的GMSMapView放在一起 05/06 12:53
goodwise: 這是iOS8 SearchController的效果 tinyurl.com/oh7auqs 05/06 12:55
goodwise: 感覺是有constraint的searchBarView對舊版sdc有影響? 05/06 12:56
goodwise: 我剛剛試了一下 searchBar加入有Navigation Controller 05/06 13:43
goodwise: 的View, 預設加入會被navigation bar擋住 05/06 13:43
goodwise: 於是設origin.y=self.topLayoutGuide.length就出現了 05/06 13:44
goodwise: 但是edit以後 彈出的tableview就又把searchbar蓋掉了 05/06 13:45
goodwise: 我將您的demo改了一下放在我的github 05/06 13:45
goodwise: http://tinyurl.com/m96oahm 05/06 13:46
goodwise: 好像找到解了 navigationBar.translucent = NO; 05/06 14:22