首页 > 解决方案 > 点击手势的值为 nil

问题描述

我正在尝试在viewForHeaderInSectionUITableView 中添加 TapGuesture。下面是中的代码

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if !isFromSearchBarData {
        let headerView = CategoryHeaderView(frame: CGRect(x:0.0, y:0.0, width: tableView.frame.size.width, height: 50.0))
        headerView.viewDictModel = arrayOfCategoryDictionary[section].values.first
        headerView.tag = section
        //Add Tap
        let headerTapped = UITapGestureRecognizer (target: self, action:#selector(sectionHeaderTapped(tapGesture:)))
        headerTapped.numberOfTapsRequired = 1
        headerView .addGestureRecognizer(headerTapped)
        return headerView
    }
    else {
        return nil
    }
}

现在行动在下面

 @objc func sectionHeaderTapped(tapGesture : UITapGestureRecognizer) {
   //...some code...//

   print("Item is :\(String(describing: tapGesture.view?.tag))")
  //SOme Code on **Tag**....//
  }

我可以看到该应用程序在模拟器上运行良好,并且我可以让 TapGesture View 运行起来。应用程序也适用于带有 iOS 11.0 的 iPhone 8 plus

但我尝试在 iOS > 11.2 的 iPhone SE 或 iPhone 8 上运行该应用程序,由于 'tapGesture.view?.tag' 为 nil,应用程序崩溃

请建议我做错了什么。

标签: iosswift

解决方案


推荐阅读