首页 > 解决方案 > 快速滑出带有子类别的栏

问题描述

表格视图中有一些行。每行打开一个新视图。现在我想在单击特定行时显示一些子类别。每个子类别都会打开不同的视图。我只使用 1 个部分。有任何想法吗?

标签: iosswift

解决方案


只需这样做。

class CategoriesListViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
var filterCategories = [CategoryViewModel]()
override func viewDidLoad() {
    super.viewDidLoad()        
    tfSearch.placeholder = LocalizeKey.search.localized        
    getCategories()
    // Do any additional setup after loading the view.
}

extension CategoriesListViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return filterCategories.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CategoryCellTableView
    let category = filterCategories[indexPath.row]
    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    if let controller : SubCategoriesListViewController = ViewControllerManager.shared.find() {
        controller.selectedCategory = self.filterCategories[indexPath.row]
        self.pushViewController(controller : controller)
    }
}

子类别将从主要类别中子类化并覆盖表视图选择方法并执行您想做的任何事情。

class SubCategoriesListViewController: CategoriesListViewController {

@IBOutlet weak var tableView: UITableView!
var filterCategories = [CategoryViewModel]()
override func viewDidLoad() {
    super.viewDidLoad()        
    // Do any additional setup after loading the view.
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let controller = DifferentViewController()
    self.pushViewController(controller : controller)
}

推荐阅读