如何在表视图中全局获取标头单元格

时间:2018-08-09 11:55:14

标签: swift

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

    let myIndexPath = IndexPath(row: 0, section: section)

    let header = tableView.dequeueReusableCell(withIdentifier: "header", for: myIndexPath) as? GuideheaderTableViewCell

     header?.dwnldBtn.isHidden = true

        header?.dwnldBtn.tag = section
        header?.dwnldBtn.addTarget(self, action:#selector(headerPressed(_:)), for:.touchUpInside)

    return header?.contentView
    }


 @objc func headerPressed(_ sender: AnyObject) {

        print("Header tapped %@", sender.tag)
       let indexPath = IndexPath(row: 0, section: sender.tag)

         if hiddenSections.contains(sender.tag) 
{
            hiddenSections.remove(at: hiddenSections.index(of: sender.tag)!)

            tableView.reloadSections(NSIndexSet(index: sender.tag) as IndexSet, with: .automatic)

            tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
        }
        else 
{
            hiddenSections.append(sender.tag)
            tableView.reloadSections(NSIndexSet(index: sender.tag) as IndexSet, with: .automatic)
        }

    }

我将dwnldBtn隐藏在标题中,如何在“ headerPressed”上显示dwnldBtn

1 个答案:

答案 0 :(得分:0)

您可以从tableView

获取节标题
let header = tableView.headerViewForSection(section: sender.tag) as! GuideheaderTableViewCell
header?.dwnldBtn.isHidden = false