我有一个包含三个部分的UIViewController,它包含一个UITableView。另外,这个UIViewController实现了在该UITableView中搜索的UISearchResultsUpdating。UIViewController看起来如下所示:
class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {
var searchController = UISearchController(searchResultsController: nil)
@IBOutlet weak var tableView: UITableView!
private struct TableViewSections {
static let sectionOne = 0
static let sectionTwo = 1
static let sectionThree = 2
}
override func viewDidLoad() {
super.viewDidLoad()
self.setupTableView()
self.setupSearchController()
}
private func setupSearchController() {
self.searchController.searchResultsUpdater = self
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.placeholder = "Search"
self.searchController.searchBar.autocapitalizationType = .sentences
self.searchController.hidesNavigationBarDuringPresentation = false
if #available(iOS 11.0, *) {
self.navigationItem.searchController = self.searchController
} else {
self.tableView.tableHeaderView = self.searchController.searchBar
}
}
private func setupTableView() {
self.tableView.delegate = self
self.tableView.dataSource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// In each of these cases i will do the work that is needed to return the correct number of rows
// Just for demonstration i return a random int in every case
if self.searchController.isActive {
switch section {
case TableViewSections.sectionOne:
return 10
case TableViewSections.sectionTwo:
return 20
case TableViewSections.sectionThree:
return 30
default:
return 0
}
} else {
switch section {
case TableViewSections.sectionOne:
return 15
case TableViewSections.sectionTwo:
return 25
case TableViewSections.sectionThree:
return 35
default:
return 0
}
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// In each of these cases i will do the work that is needed to show the correct content
// Just for demonstration i return UITableViewCell() in every case
switch indexPath.section {
case TableViewSections.sectionOne:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
case TableViewSections.sectionTwo:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
case TableViewSections.sectionThree:
if self.searchController.isActive {
return UITableViewCell()
} else {
return UITableViewCell()
}
default:
return UITableViewCell()
}
}
// MARK: UISearchResultsUpdating
func updateSearchResults(for searchController: UISearchController) {
// do the search work here...
}
}您可以看到tableView(_:numberOfRowsInSection:)和cellForRow(at:)之间的区别。
我现在的问题是:建议怎样做?更好的解决方案是tableView(_:numberOfRowsInSection:),cellForRow(at:),还是有更好的解决方案?
发布于 2018-06-22 09:07:21
numberOfRowsInSection:函数在我看来不错,将这个函数更改为更好的功能将取决于您可以结合区段参数和数据应用的可能逻辑。例如,如果您有这样的一系列字典:
let data = [ 1: ["hola", "hey"], 2: ["adiós"] ]该方法的实施可减少到:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.searchController.isActive {
let array = searchData[section]
return array.count
}
let array = data[section]
return array.count
}这确实适用于五节和一百万节。
对于cellForRowAt indexPath:函数,我首先检查搜索状态:
if self.searchController.isActive {
// handle sections for searching
} else {
// handle sections normaly
}这样做将允许您,例如,在多个节之间共享组功能。举我所举的例子来说,一个实现可能是这样的:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let text = ""
if self.searchController.isActive {
let array = searchData[indexPath.section]
text = array[indexPath.row]
} else {
let array = data[indexPath.section]
text = array[indexPath.row]
}
configureCell(with: text, at: indexPath)
}我这样做是因为我不想使代表们的执行负担过重。在configureCell:at:中,您必须实现创建单元格所需的逻辑,该逻辑以节和收到的数据为基础。
正如我前面所写的,这些方法的实现将取决于数据的结构,它可以减少或增加以预期方式显示数据所需的代码量。
https://codereview.stackexchange.com/questions/197039
复制相似问题