2015-06-07 41 views
3

enter image description here 这是之前的屏幕。如何使用ios 8 UISearchController单击搜索栏时隐藏节标题?

然后,我点击了搜索栏: enter image description here

代码如下:// MARK: - searchController

func initSearchController() { 
    self.resultSearchController = ({ 
     let controller = UISearchController(searchResultsController: nil) 
     controller.searchResultsUpdater = self 
     controller.dimsBackgroundDuringPresentation = false 
     controller.searchBar.sizeToFit() 
     controller.searchBar.backgroundColor = UIColor.clearColor() 

     self.tableView.tableHeaderView = controller.searchBar 
     return controller 
    })() 
} 

请帮助我。谢谢。

回答

2

设置部分为零的标题,如果有在这一节中没有行,这会隐藏部分的标题

// Set the title of the section header, return nil if no rows in that section 
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if self.tableView(tableView, numberOfRowsInSection: section) == 0 { 
     return nil 
    } else { 
     return "section title \(section)" 
    } 
} 

这将工作,假设tableView(tableView: UITableView, numberOfRowsInSection section: Int)正确实施,以反映部分的数量基于搜索结果。

如果你想完全消除部分的标题时,搜索栏是活动的,则只需添加这

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    if self.resultSearchController.active { 
     return nil 
    } else { 
     return "section title \(section)" 
    } 
} 
+0

谢谢,这将隐藏部分的标题。 –

+0

如果解决了问题,请接受答案。 –