UITableView 如何?搜尋方塊功能

來源:互聯網
上載者:User

                                                        UITableView  如何?搜尋方塊功能

1。定義如下兩個變數,並且聲明為屬性。

 

UISearchBar  *  searchBar;

UISearchDisplayController *  searchDc;

 

 

2。在loadView中,初始化 這兩個控制項

     - (void)loadView<br />{<br /> //在這裡建立搜尋欄和搜尋顯示控制器<br />self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];<br />self.searchBar.tintColor=[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f];<br />self.searchBar.autocorrectionType=UITextAutocorrectionTypeNo;<br />self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;<br />self.searchBar.keyboardType=UIKeyboardTypeAlphabet;<br />self.searchBar.hidden=NO;<br />self.searchBar.placeholder=[NSString stringWithCString:"請輸入需要尋找的常值內容" encoding: NSUTF8StringEncoding];<br />self.tableView_.tableHeaderView=self.searchBar;</p><p> self.searchDc=[[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] autorelease];<br />self.searchDc.searchResultsDataSource=self;<br />self.searchDc.searchResultsDelegate=self;<br />[self.searchDc setActive:NO];</p><p>} 

 

3。在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 

     函數中,對搜尋控制器做特殊化處理。

 

     - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section<br />{<br />if(tableView==tableView_)<br />{<br />//如果是普通的TableView<br /> //返回正常的cell個數<br /> }<br /> else<br /> {<br /> //如果是搜尋顯示控制器<br /> std::string strToFind=[self.searchBar.text UTF8String];<br /> //然後,根據strToFind去篩選 符合搜尋條件的結果,並且<br /> //返回合格cell的個數。<br /> }<br />} 

 

   4。在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

      中,根據 是否是搜尋控制器,返回不同的cell 資料。

 

 

   5。在  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

        等等需要特殊處理的函數中,都加上對搜尋控制器的 特殊處理。

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.