【问题标题】:how to remove table view section header using prototype cell programmatically如何以编程方式使用原型单元格删除表格视图部分标题
【发布时间】:2018-03-05 04:00:48
【问题描述】:

我使用这样的原型单元格在界面生成器中设置我的表格视图标题部分

这是这个表格视图标题部分的最终结果

数据实际上是动态的,如果可用的部分只有一个(比如说金融),我希望表格视图标题部分不存在,只显示人名。下面是我使用的简化代码。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    var sectionMember : [Department]?

    var abcd = 2


    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        tableView.delegate = self

        let IT = Department(name: "IT", member: ["joko", "santi","pipin","candra"])
        let finance = Department(name: "Finance & Accounting", member: ["ririn","andri","bagus","reyhan"])
        let security = Department(name: "security", member: ["anto","budi","rudi"])
        let purchasing = Department(name: "Purchasing", member: ["lulu","rina","santi"])

        sectionMember = [IT,finance,security, purchasing]
        //sectionMember = [IT]


    }



}

extension ViewController : UITableViewDelegate {

}

extension ViewController : UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return (sectionMember?.count)!
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sectionMember![section].member.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! TableViewCell

        let departmentMember = sectionMember![indexPath.section].member[indexPath.row]

        cell.memberOfDepartment = departmentMember

        return cell
    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {


        if abcd == 1 {
            return nil
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! HeaderCell
            cell.department = sectionMember![section].name
            return cell
        }



    }




}

为了简化我使用变量 abcd 的代码,如果 abcd = 1 我希望该部分标题被隐藏/删除/不存在,但如果 abcd 不是 1 则显示标题部分。

viewForHeaderInSection的方法中,我return nil如果abcd = 1,但结果只是仍然显示标题部分,但是颜色是灰色的,我不知道为什么它是灰色而不是粉红色在故事板中。那么如何摆脱下面的灰色部分呢?

【问题讨论】:

    标签: ios swift uitableview tableview


    【解决方案1】:

    如果只有一个部分,这里你给了标题 nil,但也给了高度

     func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    
        if sectionMember?.count ?? 0 == 1 {
            return 0.0
        }
        else {
            return your height
        }
    }
    

    还有, 你的功能

    func numberOfSections(in tableView: UITableView) -> Int {
        return (sectionMember?.count)!
    }
    

    永远不要像这样使用 bang 运算符。用默认值解开它

    func numberOfSections(in tableView: UITableView) -> Int {
        return self.sectionMember?.count ?? 0
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多