UITableView's static cell how to hidden and display

72 Views Asked by At

I wanted to hide a static cell in a UITableView, and I did that with its data source method. But when I tried to show it again, I couldn't. Here is the test code:

        var flag = false

// MARK: - Table view data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 && flag {
        return 0
    } else {
        return 44
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = super.tableView(tableView, cellForRowAt: indexPath)
        cell.frame.size = CGSize(width: cell.frame.width, height: 44.0)
        cell.alpha = 1
        cell.autoresizingMask = UIView.AutoresizingMask(rawValue: 0)
        cell.subviews.first?.frame.size = CGSize(width: cell.subviews.first?.frame.width ?? 0, height: 44.0)
        cell.subviews.first?.alpha = 1
        cell.subviews.first?.autoresizingMask = UIView.AutoresizingMask(rawValue: 0)
        print("\(cell)")
        return cell
    } else {
        return super.tableView(tableView, cellForRowAt: indexPath)
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    flag = !flag
    tableView.reloadRows(at: [IndexPath(row: 0, section: 0)], with: .automatic)
}
1

There are 1 best solutions below

1
方振兴 On

I found that I use “tableView.reloadData()" could resolve this question.but that is isn't the best way.may be that is Apple's bug?