在手势方法中获取节标题单元格

时间:2017-09-28 12:34:54

标签: ios swift uitableview uigesturerecognizer

我正在开发一个' UITableView'使用不同的节标题。节标题包含用于展开和折叠节的标签手势识别。

在章节标题视图中,我使用附件图标的图像向用户显示该部分已展开或折叠。

我担心的是当我点击节标题然后控制转到手势方法。在该方法中,我应该如何让标题单元格相应地更新图像?

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
    if self.useSearchDefinitions {
        if let ret = tableView.dequeueReusableCell(withIdentifier: INBOX_HEADER_CELL_IDENTIFIER) as? InboxHeaderCell {
            ret.contentView.backgroundColor = UIColor(red: 236 / 255.0, green: 236 / 255.0, blue: 236 / 255.0, alpha: 1.0)
            ret.contentView.tag = section
            ret.lblHeaderTitle?.textColor = UIColor(red: 110 / 255.0, green: 110 / 255.0, blue: 110 / 255.0, alpha: 1.0)
            ret.lblHeaderTitle?.font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)
            ret.lblHeaderTitle?.text = presenter.sectionTitle(section)

            ret.accessoryImage.image = UIImage(named: "inbox-expand.png")

            // Set tap gesture
            let headerViewTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.headerViewGestureHandler))
            headerViewTapRecognizer.delegate = self
            headerViewTapRecognizer.numberOfTouchesRequired = 1
            headerViewTapRecognizer.numberOfTapsRequired = 1

            ret.contentView.addGestureRecognizer(headerViewTapRecognizer)

            return ret.contentView
        }
    }

    return nil
}

这是为了获得手势

    func headerViewGestureHandler(_ sender: UIGestureRecognizer)
{
    tableView.beginUpdates()

    if let tag = sender.view?.tag {
        let section = Int(tag)
        let shouldCollapse: Bool = !collapsedSections.contains((section))
        let numOfRows = Int(presenter.numberOfRows(tag))
}

}

如何在此方法中获取特定的单击部分标题单元格,以便我可以相应地更新图像?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我建议:

  1. 将Gesture代码放在您的部分标题
  2. 使用“回叫”闭包将点按回传给视图控制器
  3. 这是一个简单的例子(假设您有一个带有表视图的View Controller,通过IBOutlet连接):

    class SimpleSectionHeaderView: UITableViewHeaderFooterView, UIGestureRecognizerDelegate {
    
        // typical UILabel
        var lblHeaderTitle: UILabel = {
            let v = UILabel()
            v.translatesAutoresizingMaskIntoConstraints = false
            return v
        }()
    
        // this is our "call back" closure    
        var headerTapCallback: (() -> ())?
    
        func headerViewGestureHandler(_ sender: UIGestureRecognizer) {
            // just for debugging, so we know the tap was triggered
            print("tapped!!!")
    
            // "call back" to the view controller
            headerTapCallback?()
        }
    
        func commonInit() {
    
            // set our backgroundColor
            contentView.backgroundColor = .cyan
    
            // add a label and set its constraints
            self.addSubview(lblHeaderTitle)
            lblHeaderTitle.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8.0).isActive = true
            lblHeaderTitle.centerYAnchor.constraint(equalTo: self.centerYAnchor, constant: 0.0).isActive = true
    
            // Set tap gesture
            let headerViewTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.headerViewGestureHandler))
            headerViewTapRecognizer.delegate = self
            headerViewTapRecognizer.numberOfTouchesRequired = 1
            headerViewTapRecognizer.numberOfTapsRequired = 1
    
            // add it to self
            self.addGestureRecognizer(headerViewTapRecognizer)
    
        }
    
        override init(reuseIdentifier: String?) {
            super.init(reuseIdentifier: reuseIdentifier)
            commonInit()
        }
    
        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }
    
    }
    
    class TableWithSectionHeadersViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        @IBOutlet weak var theTableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // standard cell registration
            theTableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
    
            theTableView.register(SimpleSectionHeaderView.self, forHeaderFooterViewReuseIdentifier: "simpleHeaderView")
    
            // make sure these are set (in case we forgot in storyboard)
            theTableView.delegate = self
            theTableView.dataSource = self
    
        }
    
        func handleHeaderTap(_ section: Int) -> Void {
    
            // do whatever we want based on which section header was tapped
            print("View Controller received a \"tapped\" in header for section:", section)
    
        }
    
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
            let v = tableView.dequeueReusableHeaderFooterView(withIdentifier: "simpleHeaderView") as! SimpleSectionHeaderView
    
            // set the view's label text
            v.lblHeaderTitle.text = "Section \(section)"
    
            // set the view's "call back" closure
            v.headerTapCallback = {
                _ in
                self.handleHeaderTap(section)
            }
    
            return v
        }
    
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 60;
        }
    
        // MARK: - Table view data source
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 5
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 4
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
    
            // Configure the cell...
            cell.textLabel?.text = "\(indexPath)"
    
            return cell
        }
    
    }
    

    这也消除了设置任何.tag属性的需要(由于多种原因,这通常是一个坏主意)。