首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不同截面的UITableViewDataSource与SearchController

不同截面的UITableViewDataSource与SearchController
EN

Code Review用户
提问于 2018-06-22 07:27:29
回答 1查看 80关注 0票数 2

我有一个包含三个部分的UIViewController,它包含一个UITableView。另外,这个UIViewController实现了在该UITableView中搜索的UISearchResultsUpdatingUIViewController看起来如下所示:

代码语言:javascript
运行
复制
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:),还是有更好的解决方案?

EN

回答 1

Code Review用户

发布于 2018-06-22 09:07:21

numberOfRowsInSection:函数在我看来不错,将这个函数更改为更好的功能将取决于您可以结合区段参数和数据应用的可能逻辑。例如,如果您有这样的一系列字典:

代码语言:javascript
运行
复制
let data = [ 1: ["hola", "hey"], 2: ["adiós"] ]

该方法的实施可减少到:

代码语言:javascript
运行
复制
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:函数,我首先检查搜索状态:

代码语言:javascript
运行
复制
if self.searchController.isActive {
    // handle sections for searching
} else {
    // handle sections normaly
}

这样做将允许您,例如,在多个节之间共享组功能。举我所举的例子来说,一个实现可能是这样的:

代码语言:javascript
运行
复制
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:中,您必须实现创建单元格所需的逻辑,该逻辑以节和收到的数据为基础。

正如我前面所写的,这些方法的实现将取决于数据的结构,它可以减少或增加以预期方式显示数据所需的代码量。

票数 4
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/197039

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档