迅速:仅在多个部分时显示部分标题

时间:2019-07-04 07:13:31

标签: ios swift uitableview

我有一个表,其中的节数可变。如果要显示一个以上的自定义标题,则有多个部分,但是如果只有一个,则根本不需要标题。 我的方法是使用func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?,如果我只有一个部分,则返回nil。但是,我没有显示任何标题,而是得到了一个空标题。有什么想法吗?

这是我的代码

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Orders"

    tableView.dataSource = self
    tableView.delegate = self


    let headerNib = UINib.init(nibName: "SectionHeader", bundle: Bundle.main)
    tableView.register(headerNib, forHeaderFooterViewReuseIdentifier: "SectionHeader")

}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if(ordersList.count < 2) {return nil}
    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! SectionHeader
    headerView.label.text = "\(sectionHeaders[section]) (\(ordersList[section].count))"
    headerView.button.setTitle(self.collapsed[section] ? "▶" : "▼", for: .normal)
    headerView.onTap = {
        self.collapsed[section] = !self.collapsed[section]
        headerView.button.rotate(self.collapsed[section] ? CGFloat(-.pi/2.0) : CGFloat(.pi/2.0), duration: 0.1, completion: {
            self.tableView.beginUpdates()
            self.tableView.reloadSections([section], with: .fade)

            self.tableView.endUpdates()
        })
    }

    return headerView
}

4 个答案:

答案 0 :(得分:0)

也使用heightForHeaderInSection委托方法。如果条件不满足,则将高度设为0。供参考-

public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    if(ordersList.count < 2) {return 0.1}
    else { return HEIGHT} //custom value of view height
}

答案 1 :(得分:0)

您必须使用以下方法来获得所需的结果:

func numberOfSections(in tableView: UITableView) -> Int {
        if(ordersList.count < 2) {return nil}
        return ordersList.count
    }

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if(ordersList.count < 2) {return nil} 
      //else return some view }


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(ordersList.count < 2) {return 0}
         return some_height

    }

答案 2 :(得分:0)

使用tableView.numberOfSections获取节计数。并在tableView(_:viewForHeaderInSection:)中使用该值来检查是否要返回SectionHeadernil。如果您不想在特定的nil中显示header,则需要返回section

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    guard tableView.numberOfSections > 1 else {
        return nil
    }
    let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "SectionHeader") as! SectionHeader
    //add rest of the code...
    return headerView
}

实施tableView(_:heightForHeaderInSection:)以基于header height返回section。如果您不想在0中显示header,请返回section,否则将需要default header height

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return (tableView.numberOfSections > 1) ? 100.0 : 0.0
}

答案 3 :(得分:-1)

您应该重写tableview的numberOfSections方法。而且,不应将dequeueReusableHeaderFooterView用于节标题视图。节标题视图应为自定义视图。

-numberOfSections

func numberOfSections(in tableView: UITableView) -> Int {
    return sectionCount
}

-sectionHeaderView

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // you can store data on a view model
    let cellViewModel = viewModel.returnViewModel(index: section)

    switch cellViewModel.type {
    case .sampleViewModel1:
        return UIView() // sampleView1
    case .sampleViewModel2:
        return UIView() // sampleView2
    }
}